YL
BAN USERthis will not work if
char [][] maze = {{'a','b','c','d','e'},
{'f','g','h','i','j'},
{'k','l','m','n','o'},
{'p','q','r','s','t'},
{'u','v','w','x','y'},
{'z'}};
I think just change the print order a little bit, then it will work:
yMove<0
=> xMove<0
=> yMove>0
=>xMove<0
use LinkedHashMap<String, int[]> to store data in java.
public static void getProdCount(String[][] input){
LinkedHashMap<String, int[]> map = new LinkedHashMap<String, int[]>();
for(int i=0;i<input.length;i++){
int[] list= map.get(input[i][0]);
if(list==null){
list = new int[12];
}
String date = input[i][1]; //2/1/1980
int index = Integer.parseInt(date.split("/")[0])-1;
list[index] = list[index]+1;
map.put(input[i][0],list);
}
for(Map.Entry<String, int[]> entry:map.entrySet()){
int[] list = entry.getValue();
for(int i=0;i<list.length;i++){
if(list[i]>0)
System.out.println(entry.getKey()+" "+getMonth(i) +" "+list[i]);
}
}
}
public static String getMonth(int i){
String[] month = {"Jan","Feb","March","April","May","June","July","Aug","Sep","Oct","Nov","Dec"};
return month[i];
}
pass input as :
String[][] input = {
{"A","1/1/1980"},
{"B","1/1/1980"},
{"C","1/1/1980"},
{"A","1/1/1980"},
{"B","1/1/1980"},
{"C","2/1/1980"},
{"A","7/1/1980"},
{"B","1/1/1980"},
{"C","5/1/1980"},
{"A","3/1/1980"},
{"B","1/1/1980"},
{"C","2/1/1980"}};
use recursion. first represend two numbers in string with same length, which means to add "0" before head for the shorter number. Then add digits by recursive:
f(n) = f(n-1).subString(1) + sum of n's digits +1 or f(n) = f(n-1).subString(0,1) + sum of n's digits. This does not work for negative numbers.
public static String add(String a , String b){
String result = "";
int lengthA = a.length();
int lengthB = b.length();
if(lengthA>lengthB){
int x = lengthA - lengthB ;
String s0 = "";
for(int i=0;i<x;i++)
s0 +="0";
b = s0+b;
} else if(lengthA<lengthB){
int x = lengthB - lengthA ;
String s0 = "";
for(int i=0;i<x;i++)
s0 +="0";
a = s0+a;
}
result = calculate(a,b);
return result;
}
public static String calculate(String a , String b){
if(a.length()==0) return "";
if(a.length()==1){
return String.valueOf(Integer.parseInt(a)+Integer.parseInt(b));
}
String str = calculate(a.substring(1), b.substring(1, b.length()));
if(str.length()==a.length()){
return String.valueOf(Integer.parseInt(a.substring(0,1))+Integer.parseInt(b.substring(0,1))+1)+str.substring(1) ;
}else{
return String.valueOf(Integer.parseInt(a.substring(0,1))+Integer.parseInt(b.substring(0,1)))+str ;
}
}
If it is only one star, then only need to compare if the star & end equals the two parts in the pattern seperated by "*".
- YL February 06, 20141. String[] strArr= pattern.split("\\*"); get two patterns for 'start with' and 'end with'.
2. just see if the string is start with strArr[0], and end with strArr[1].
3. some special cases, like empty...