This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public boolean isValidSudoku(char[][] board) { | |
| for(int i = 0; i < 9; i++){ | |
| HashSet<Character> row = new HashSet<Character>(); | |
| HashSet<Character> col = new HashSet<Character>(); | |
| HashSet<Character> cube = new HashSet<Character>(); | |
| for(int j = 0; j < 9; j++){ | |
| if(board[i][j] != '.' && !row.add(board[i][j])){ | |
| return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int lengthOfLongestSubstring(String s) { | |
| int start = 0; | |
| int end = 0; | |
| HashMap<Character, Integer> dict = new HashMap<Character, Integer>(); | |
| int res = 0; | |
| while(end < s.length()){ | |
| char c = s.charAt(end); | |
| if( dict.containsKey(c) && start <= dict.get(c)){ | |
| start = dict.get(c) + 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public String minWindow(String s, String t) { | |
| if (s == null || s.length() < 1 || t == null || t.length() < 1) return ""; | |
| int end = 0, start = 0, count = t.length(); | |
| int[] vector = new int[256]; | |
| for (char c : t.toCharArray()) { | |
| vector[c]++; | |
| } | |
| char[] str = s.toCharArray(); | |
| String res = ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public String addBinary(String a, String b) { | |
| StringBuilder sb = new StringBuilder(); | |
| int i = a.length() - 1, j = b.length() -1, carry = 0; | |
| while (i >= 0 || j >= 0) { | |
| int sum = carry; | |
| if (j >= 0) sum += b.charAt(j--) - '0'; | |
| if (i >= 0) sum += a.charAt(i--) - '0'; | |
| sb.append(sum % 2); | |
| carry = sum / 2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int lengthOfLastWord(String s) { | |
| int res = 0; | |
| s = s.trim(); | |
| if(s == "") return res; | |
| for(int i = s.length() - 1; i >= 0; i--){ | |
| if(s.charAt(i) == ' '){ | |
| res = s.length() - i - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public List<String> fullJustify(String[] words, int maxWidth) { | |
| List<String> res = new ArrayList<>(); | |
| List<String> buf = new ArrayList<>(); | |
| int len = 0; | |
| for (String word: words) { | |
| if (buf.size() + len + word.length() > maxWidth) { | |
| res.add(justify(buf, len, maxWidth)); | |
| buf = new ArrayList<>(); | |
| len = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public boolean isPowerOfFour(int num) { | |
| //return ((num - 1) & num) == 0 && ( num - 1 ) % 3 == 0; | |
| // x^n - 1的因式分解一定含有(x-1). 因为x = 1是 x^n - 1 = 0的一个根 | |
| return ((num - 1) & num) == 0 && (num & 0x55555555) != 0; | |
| //The basic idea is from power of 2, We can use "n&(n-1) == 0" to determine if n is power of 2. | |
| //0x55555555 is to get rid of those power of 2 but not power of 4 | |
| //so that the single 1 bit always appears at the odd position | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int maxProduct(String[] words) { | |
| int res = 0; | |
| if(words == null || words.length == 0) return res; | |
| int len = words.length; | |
| int[] vectors = new int[len]; | |
| for(int i = 0; i < len; i++){ | |
| String temp = words[i]; | |
| for(int j = 0; j < temp.length(); j++){ | |
| vectors[i] |= 1 << (temp.charAt(j) - 'a'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*the SUM of Arithmetic sequence */ | |
| public class Solution { | |
| public int missingNumber(int[] nums) { | |
| int len = nums.length; | |
| int sum = (0 + len)*(len + 1) / 2; | |
| for(int i = 0; i < nums.length; i++){ | |
| sum -= nums[i]; | |
| } | |
| return sum; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public String longestPalindrome(String s) { | |
| String res = ""; | |
| int resLen = 0; | |
| int len = s.length(); | |
| int[][] dp = new int[len][len]; | |
| for(int i = len - 1; i >= 0; i--){ |