Skip to content

Instantly share code, notes, and snippets.

@xynophon
xynophon / ReverseWordsinaString.java
Last active August 29, 2015 14:27
LeetCode Reverse Words in a String
import java.util.*;
public class ReverseWordsinaString {
public static String reverseWords(String s) {
String[] strs = s.split(" ");
int n = strs.length;
StringBuilder sb = new StringBuilder();
for (int i = n-1; i >= 0 ; i--) {
if(!strs[i].equals("")) {
if (sb.length() > 0) {
@xynophon
xynophon / BinaryTreeUpsideDown.java
Created August 15, 2015 15:13
LeetCode BinaryTreeUpsideDown
import java.util.*;
public class BinaryTreeUpsideDown {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
@xynophon
xynophon / PaintHouse.java
Created August 15, 2015 13:04
LeetCode PaintHouse
import java.util.*;
public class PaintHouse {
public int minCost(int[][] costs) {
int n = costs.length;
if(n == 0) return 0;
for (int i = 1; i < n; i++) {
costs[i][0] = costs[i][0] + Math.min(costs[i-1][1], costs[i-1][2]);
costs[i][1] = costs[i][1] + Math.min(costs[i-1][0], costs[i-1][2]);
costs[i][2] = costs[i][2] + Math.min(costs[i-1][0], costs[i-1][1]);
@xynophon
xynophon / Permutations.java
Created August 15, 2015 13:03
LeetCode Permutations
import java.util.*;
public class Permutations {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> l0 = new ArrayList<>();
l0.add(nums[0]);
result.add(l0);
for (int i = 1; i < nums.length; i++) {
@xynophon
xynophon / BSTIterator.java
Created July 11, 2015 06:32
LeetCode Binary Search Tree Iterator
import java.util.*;
public class BSTIterator {
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
@xynophon
xynophon / InvertBinaryTree.java
Created July 10, 2015 05:04
LeetCode Invert Binary Tree
import java.util.*;
public class InvertBinaryTree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
@xynophon
xynophon / BinaryTreeInorderTraversal.java
Created July 9, 2015 02:56
LeetCode. Binary Tree Inorder Traversal recursive, iterative
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
@xynophon
xynophon / BinaryTreePreorderTraversal.java
Last active August 29, 2015 14:24
LeetCode. Binary Tree Preorder Traversal. Recursive
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
@xynophon
xynophon / NumberOf1Bits.java
Last active August 29, 2015 14:24
LeetCode. Number Of 1Bits. JDK7
import java.util.*;
public static int hammingWeight(int n) {
int cnt = 0;
while(n != 0){
if(0 != n%2){ //if(1 == n%2){
cnt++;
}
n = n >>> 1;
}
@xynophon
xynophon / StackList.java
Last active August 29, 2015 14:24
CrackingCodingInterview. Implement Stack with LinkedList
class StackList {
Node top;
void push(Object data){
Node t = new Node(data);
t.next = top;
top = t;
}
Object pop(){