Skip to content

Commit

Permalink
add 4 new codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tg123 committed Aug 16, 2015
1 parent 2154331 commit 586de58
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 0 deletions.
Empty file added add-digits/README.md
Empty file.
5 changes: 5 additions & 0 deletions add-digits/Solution.java
@@ -0,0 +1,5 @@
public class Solution {
public int addDigits(int num) {
return num - (num - 1) / 9 * 9;
}
}
7 changes: 7 additions & 0 deletions add-digits/index.md
@@ -0,0 +1,7 @@
---
layout: solution
title: Add Digits
date: 2015-08-17 00:35:59+08:00
leetcode_id: 258
---
{% include_relative README.md %}
Empty file added binary-tree-paths/README.md
Empty file.
38 changes: 38 additions & 0 deletions binary-tree-paths/Solution.java
@@ -0,0 +1,38 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

List<String> merge(int v, List<String> subPath){
return subPath.stream()
.map(p -> v + "->" + p)
.collect(Collectors.toList());
}

public List<String> binaryTreePaths(TreeNode root) {
List<String> path = new ArrayList<>();

if(root == null) return path;

if(root.left == null && root.right == null) {
// leaf
return Arrays.asList("" + root.val);
}

if(root.left != null){
path.addAll(merge(root.val, binaryTreePaths(root.left)));
}

if(root.right != null) {
path.addAll(merge(root.val, binaryTreePaths(root.right)));
}

return path;
}
}
7 changes: 7 additions & 0 deletions binary-tree-paths/index.md
@@ -0,0 +1,7 @@
---
layout: solution
title: Binary Tree Paths
date: 2015-08-16 23:34:16+08:00
leetcode_id: 257
---
{% include_relative README.md %}
Empty file added paint-house/README.md
Empty file.
45 changes: 45 additions & 0 deletions paint-house/Solution.java
@@ -0,0 +1,45 @@
public class Solution {
static final int RED = 0b001;
static final int BLUE = 0b010;
static final int GREEN = 0b100;

static final int NONE = 0b000;
static final int ALL = 0b111;

static final int[] COLORS = {RED, BLUE, GREEN};

int index(int color){
return color / 2;
}

int min(int[] costs, int exclude){
int includes = ~(ALL & exclude);

int min = Integer.MAX_VALUE;
for(int c : COLORS){
if((c & includes) == c){
min = Math.min(costs[index(c)], min);
}
}

return min;
}

public int minCost(int[][] costs) {
if(costs.length == 0) return 0;

int[][] minCosts = new int[costs.length][COLORS.length];

for(int c : COLORS){
minCosts[0][index(c)] = costs[0][index(c)];
}

for(int i = 1; i < costs.length; i++){
for(int c : COLORS){
minCosts[i][index(c)] = costs[i][index(c)] + min(minCosts[i - 1], c);
}
}

return min(minCosts[costs.length - 1], NONE);
}
}
7 changes: 7 additions & 0 deletions paint-house/index.md
@@ -0,0 +1,7 @@
---
layout: solution
title: Paint House
date: 2015-08-17 00:33:10+08:00
leetcode_id: 256
---
{% include_relative README.md %}
Empty file.
31 changes: 31 additions & 0 deletions verify-preorder-sequence-in-binary-search-tree/Solution.java
@@ -0,0 +1,31 @@
public class Solution {
public boolean verifyPreorder(int[] preorder) {
int[] inorder = Arrays.copyOf(preorder, preorder.length);
Arrays.sort(inorder);

LinkedList<Integer> stack = new LinkedList<>(); // fuck

stack.push(0);
stack.push(inorder.length);

for(int p = 0; p < preorder.length; /*void*/){
int ed = stack.pop();
int st = stack.pop();

if(st >= ed) continue;

int root = preorder[p++];

int i = Arrays.binarySearch(inorder, st, ed, root);
if(i < 0) return false;

stack.push(i + 1);
stack.push(ed);

stack.push(st);
stack.push(i);
}

return true;
}
}
7 changes: 7 additions & 0 deletions verify-preorder-sequence-in-binary-search-tree/index.md
@@ -0,0 +1,7 @@
---
layout: solution
title: verify-preorder-sequence-in-binary-search-tree/
date: 2015-08-15 11:47:30+08:00
leetcode_id: 255
---
{% include_relative README.md %}

0 comments on commit 586de58

Please sign in to comment.