Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / WiggleSort.java
Created April 13, 2016 12:31
LeetCode - Wiggle Sort
// 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);
@cangoal
cangoal / PalindromePermutation.java
Created April 13, 2016 04:56
LeetCode - Palindrome Permutation
// 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);
@cangoal
cangoal / FlipGameII.java
Created April 13, 2016 04:46
LeetCode - Flip Game II
// 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) {
@cangoal
cangoal / FlipGame.java
Created April 13, 2016 03:23
LeetCode - Flip Game
// 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:
// [
// "--++",
// "+--+",
// "++--"
@cangoal
cangoal / ZigzagIterator.java
Last active April 21, 2016 02:45
LeetCode - Zigzag Iterator
// 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?
@cangoal
cangoal / InterleavingPositiveandNegativeNumbers.java
Created April 12, 2016 22:50
LintCode - Interleaving Positive and Negative Numbers
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--);
@cangoal
cangoal / DigitCounts.java
Created April 12, 2016 20:14
LintCode - Digit Counts
// 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;
@cangoal
cangoal / HashFunction.java
Created April 12, 2016 13:36
LintCode - Hash Function
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;
@cangoal
cangoal / MatrixZigzagTraversal.java
Created April 12, 2016 04:11
LintCode - Matrix Zigzag Traversal
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++];
@cangoal
cangoal / PaintHouseII.java
Last active April 21, 2016 14:23
LeetCode - Paint House II
// 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;