Wednesday, December 2, 2015

[leetcode]Word Pattern

最重要捉住1 to 1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Solution {
    public boolean wordPattern(String pattern, String str) {
        if (pattern.length() == 0 || str.length() == 0) return false;

        String[] array = str.split(" ");
        if (array.length != pattern.length()) return false;
        HashMap<String, Character> lookup = new HashMap<String, Character>();
        HashMap<Character, String> reverseLookup = new HashMap<Character, String>();
        String result = "";
        for (int i = 0; i < array.length; i++){
            if(reverseLookup.containsKey(pattern.charAt(i)) && !reverseLookup.get(pattern.charAt(i)).equals(array[i])){
                return false;
            }else{
                reverseLookup.put(pattern.charAt(i), array[i]);
            }
            
            if(lookup.containsKey(array[i]) && lookup.get(array[i]) != pattern.charAt(i)){
                return false;    
            }else{
                lookup.put(array[i], pattern.charAt(i));
            }
        }
        return true;
    }
}

[leetcode]H-index

这题有点绕啊 别想太多 就用wiki的方法 sort好 从大到小看... 假如有index >= collection[index]的 那就return index, 最后return size
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Solution {
    public int hIndex(int[] citations) {
        if (citations == null || citations.length == 0) return 0;
        int[] copy = Arrays.copyOf(citations, citations.length);
        Arrays.sort(copy);
        for (int i = copy.length-1, j = 0; i >=0; i--, j++){
            if (j>=copy[i]) return j;
        }
        return copy.length;
    }
}

[leetcode]Isomorphic Strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) return false;
        HashMap<Character, Character>lookup = new HashMap<Character, Character>();
        HashMap<Character, Character>lookup2 = new HashMap<Character, Character>();        
        for (int i = 0; i < s.length(); i++){
            char c1 = s.charAt(i);
            char c2 = t.charAt(i);
            if (lookup.containsKey(c1) && lookup.get(c1) != c2) return false;
            if (lookup2.containsKey(c2) && lookup2.get(c2) != c1) return false;
            lookup.put(c1, c2);
            lookup2.put(c2,c1);
        }
        return true;
    }
}

[leetcode]Valid Sudoku

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Solution {
    public boolean isValidSudoku(char[][] board) {
        if (board == null || board.length != 9 || board[0].length != 9) return false;
        boolean lookup[];
        boolean lookCol[];
        for (int i = 0; i < 9; i++){
         lookup = new boolean[9];
         lookCol = new boolean[9];
         for (int j = 0; j < 9; j++){
          if (board[i][j] != '.'){
           int index = board[i][j] - '0'-1;
           if (lookup[index]) return false;
           lookup[index] = true;
          }
          if (board[j][i] != '.'){
           int index2 = board[j][i] -'0'-1;
           if (lookCol[index2]) return false;
           lookCol[index2] = true;
          }
         }
        }
        
        for (int i = 0; i < 9; i+=3){
         for (int j = 0; j < 9; j+=3){
          lookup = new boolean[9];
          lookCol = new boolean[9];
          for (int k = i; k < i+3; k++){
           for (int q = j; q < j+3; q++){
            if (board[k][q] != '.'){
             int index = board[k][q] - '0'-1;
             if (lookup[index]) return false;
             lookup[index] = true;
            }
            if (board[q][k] != '.'){
             int index2 = board[q][k] -'0'-1;
             if (lookCol[index2]) return false;
             lookCol[index2] = true;
            }
           }
          }
         }
        }       
        return true; 
    }
}

[leetcode]Strobogrammatic Number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
 boolean rightMapping(char c1, char c2){
  if (c1 > c2) return rightMapping(c2,c1);
  if (c1 == '0') return c2 == '0';
  if (c1 == '1') return c2 == '1';
  if (c1 == '6') return c2 == '9';
  if (c1 == '8') return c2 == '8';
  return false;
 }

    public boolean isStrobogrammatic(String num) {
     char[] arr = num.toCharArray();
     int left = 0;
     int right = arr.length-1;
     while (left <= right){
      if (!rightMapping(arr[left++], arr[right--])) return false;
     }
     return true;
    }
}

[leetcode] happy number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution {
 private int getNext(int n){
  int sum = 0;
  while (n > 0){
   int digit = n%10;
   sum += (digit*digit);
   n/=10;
  }
  return sum;
 }

    public boolean isHappy(int n) {
     if (n <= 0) return false;
   HashSet<Integer> lookup = new HashSet<Integer>();  
   while (n > 1){
    if (lookup.contains(n)){
     return false;
    }
    lookup.add(n);
    n = getNext(n);
   }
   return true;
    }
}

Tuesday, December 1, 2015

[leetcode]Shortest Word Distance II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class WordDistance {
    HashMap<String, List<Integer>> lookup = new HashMap<String, List<Integer>>();
    public WordDistance(String[] words) {
        for (int i = 0; i < words.length; i++){
            if (!lookup.containsKey(words[i])){
                lookup.put(words[i], new ArrayList<Integer>());
            }
            lookup.get(words[i]).add(i);
        }
    }

    public int shortest(String word1, String word2) {
        List<Integer> list1 = lookup.get(word1);
        List<Integer> list2 = lookup.get(word2);
        int min = Integer.MAX_VALUE;
        for (int i = 0, j = 0; i < list1.size() && j < list2.size();){
            min = Math.min(min, Math.abs(list1.get(i)-list2.get(j)));
            if (list1.get(i) < list2.get(j)) i++;
            else j++;
        }
        return min;
    }
}