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
| // Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... | |
| // For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. | |
| public void wiggleSort(int[] nums) { | |
| if(nums == null || nums.length == 0) return; | |
| for(int i = 0; i < nums.length-1; i++){ | |
| if(i % 2 == 0 && nums[i] > nums[i+1]){ | |
| swap(nums, i, i+1); | |
| }else if(i % 2 == 1 && nums[i] < nums[i+1]){ | |
| swap(nums, i, 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
| // Given a string, determine if a permutation of the string could form a palindrome. | |
| // For example, | |
| // "code" -> False, "aab" -> True, "carerac" -> True. | |
| public boolean canPermutePalindrome(String s) { | |
| if(s == null) return false; | |
| Set<Character> set = new HashSet<Character>(); | |
| for(int i = 0; i < s.length(); i++){ | |
| char c = s.charAt(i); |
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
| // You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner. | |
| // Write a function to determine if the starting player can guarantee a win. | |
| // For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+". | |
| // Follow up: | |
| // Derive your algorithm's runtime complexity. | |
| public boolean canWin(String s) { |
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
| // You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner. | |
| // Write a function to compute all possible states of the string after one valid move. | |
| // For example, given s = "++++", after one move, it may become one of the following states: | |
| // [ | |
| // "--++", | |
| // "+--+", | |
| // "++--" |
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
| // Given two 1d vectors, implement an iterator to return their elements alternately. | |
| // For example, given two 1d vectors: | |
| // v1 = [1, 2] | |
| // v2 = [3, 4, 5, 6] | |
| // By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6]. | |
| // Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases? |
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 void rerange(int[] A) { | |
| // write your code here | |
| if(A == null || A.length == 0) return; | |
| int left = 0, right = A.length - 1; | |
| while(true){ | |
| while(left < right && A[left] < 0) left++; | |
| while(left < right && A[right] >0) right--; | |
| if(left >= right) break; | |
| swap(A, left++, right--); |
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
| // solution 1 | |
| public int digitCounts(int k, int n) { | |
| // write your code here | |
| if(k < 0 || n < 0) return 0; | |
| if(k == 0 && n == 0) return 1; | |
| int factor = 1, count = 0; | |
| while(factor <= n){ | |
| int low = n % factor; |
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 int hashCode(char[] key,int HASH_SIZE) { | |
| // write your code here | |
| if(key == null || key.length == 0) return -1; | |
| long hashValue = 0, factor = 1; | |
| for(int i = key.length - 1; i >= 0; i--){ | |
| hashValue += key[i] % HASH_SIZE * factor % HASH_SIZE; | |
| hashValue %= HASH_SIZE; | |
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 int[] printZMatrix(int[][] matrix) { | |
| // write your code here | |
| int m = matrix.length, n = matrix[0].length; | |
| int[] res = new int[m * n]; | |
| int x = 0, y = 0, size = 0; | |
| while(size < m * n){ | |
| while(x >= 0 && y < n){ | |
| res[size++] = matrix[x--][y++]; |
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
| // O(m*k*k) | |
| public int minCostII(int[][] costs) { | |
| // Write your code here | |
| if(costs.length == 0 || costs[0].length == 0) return 0; | |
| int m = costs.length, n = costs[0].length; | |
| for(int i=1; i<m; i++){ | |
| for(int j=0; j<n; j++){ | |
| int min = Integer.MAX_VALUE; | |
| for(int k=0; k<n; k++){ | |
| if(k == j) continue; |