Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Solution { | ||
public int addDigits(int num) { | ||
return num - (num - 1) / 9 * 9; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
31
verify-preorder-sequence-in-binary-search-tree/Solution.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |