Skip to content

Commit 4a97391

Browse files
add 623 and change names
1 parent 2322a22 commit 4a97391

32 files changed

+244
-72
lines changed

README.md

Lines changed: 26 additions & 25 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
* Created by stevesun on 6/17/17.
10+
*/
11+
public class Contest {
12+
13+
14+
public TreeNode addOneRow(TreeNode root, int v, int d) {
15+
Queue<TreeNode> queue = new LinkedList<>();
16+
Queue<TreeNode> prevQueue = new LinkedList<>();
17+
queue.offer(root);
18+
int depth = 1;
19+
while (!queue.isEmpty()) {
20+
int size = queue.size();
21+
for (int i = 0; i < size; i++) {
22+
TreeNode curr = queue.poll();
23+
if (curr.left != null && depth == (d-1)) {
24+
prevQueue.offer(curr.left);
25+
queue.offer(curr.left);
26+
} else if (curr.left == null && depth == d) {
27+
queue.offer(new TreeNode(Integer.MIN_VALUE));
28+
} else if (curr.left != null) {
29+
queue.offer(curr.left);
30+
}
31+
32+
if (curr.right != null && depth == (d-1)) {
33+
prevQueue.offer(curr.right);
34+
queue.offer(curr.right);
35+
} else if (curr.right == null && depth == d) {
36+
queue.offer(new TreeNode(Integer.MIN_VALUE));
37+
} else if (curr.right != null) {
38+
queue.offer(curr.right);
39+
}
40+
}
41+
if (depth == d) {
42+
break;
43+
}
44+
depth++;
45+
}
46+
Queue<TreeNode> secondQueue = new LinkedList<>();
47+
secondQueue.offer(root);
48+
depth = 0;
49+
while (!secondQueue.isEmpty()) {
50+
depth++;
51+
int size = secondQueue.size();
52+
for (int i = 0; i < size; i++) {
53+
TreeNode curr = queue.poll();
54+
if (curr.left != null) {
55+
queue.offer(curr.left);
56+
}
57+
}
58+
}
59+
return root;
60+
}
61+
62+
63+
public static void main(String... args) {
64+
Contest contest = new Contest();
65+
System.out.println("hello world!");
66+
// int[][] arrays = new int[][]{
67+
// {1,2,3},
68+
// {4,5},
69+
// {1,2,3},
70+
// };
71+
}
72+
}

src/main/java/com/fishercoder/solutions/BalancedBinaryTree.java renamed to src/main/java/com/fishercoder/solutions/_110.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.*/
99

10-
public class BalancedBinaryTree {
10+
public class _110 {
1111

1212
class Solution_1 {
1313
//recursively get the height of each subtree of each node, compare their difference, if greater than 1, then return false

src/main/java/com/fishercoder/solutions/BestTimeToBuyAndSellStockII.java renamed to src/main/java/com/fishercoder/solutions/_122.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**Say you have an array for which the ith element is the price of a given stock on day i.
44
55
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).*/
6-
public class BestTimeToBuyAndSellStockII {
6+
public class _122 {
77

88
/**It turns out to be a super simple one, it's really a greedy one! Just keep being greedy if it's possible.*/
99
public int maxProfit(int[] prices) {

src/main/java/com/fishercoder/solutions/BestTimeToBuyAndSellStockIII.java renamed to src/main/java/com/fishercoder/solutions/_123.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
Note:
1010
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).*/
11-
public class BestTimeToBuyAndSellStockIII {
11+
public class _123 {
1212

1313
//this is a very clear solution and very highly upvoted in Discuss, but not extensibel to K solution.
1414
public int maxProfit(int[] prices) {
@@ -87,7 +87,7 @@ public static void main(String...strings){
8787
// int[] prices = new int[]{6,1,3,2,4,7};
8888
// int[] prices = new int[]{1,2,4,2,5,7,2,4,9,0};//(7-2)+(9-2) = 5+7 = 12 is wrong, it should be (7-1)+(9-2) = 6+7 = 13
8989
int[] prices = new int[]{2,5,7,1,4,3,1,3};
90-
BestTimeToBuyAndSellStockIII test = new BestTimeToBuyAndSellStockIII();
90+
_123 test = new _123();
9191
System.out.println(test.maxProfit(prices));
9292
}
9393

src/main/java/com/fishercoder/solutions/CloneGraph.java renamed to src/main/java/com/fishercoder/solutions/_133.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
\_/
3333
3434
*/
35-
public class CloneGraph {
35+
public class _133 {
3636

3737
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
3838
if(node == null) return node;

src/main/java/com/fishercoder/solutions/Candy.java renamed to src/main/java/com/fishercoder/solutions/_135.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Children with a higher rating get more candies than their neighbors.
1010
What is the minimum candies you must give?
1111
*/
12-
public class Candy {
12+
public class _135 {
1313

1414
public int candy(int[] ratings) {
1515
int[] candy = new int[ratings.length];

src/main/java/com/fishercoder/solutions/SingleNumberII.java renamed to src/main/java/com/fishercoder/solutions/_137.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
1111
1212
*/
13-
public class SingleNumberII {
13+
public class _137 {
1414

1515
public int singleNumber(int[] nums) {
1616
Map<Integer, Integer> map = new HashMap();
@@ -25,7 +25,7 @@ public int singleNumber(int[] nums) {
2525

2626
public static void main(String...strings){
2727
int[] nums = new int[]{2,2,3,2};
28-
SingleNumberII test = new SingleNumberII();
28+
_137 test = new _137();
2929
System.out.println(test.singleNumber(nums));
3030
}
3131

src/main/java/com/fishercoder/solutions/ContainsDuplicate.java renamed to src/main/java/com/fishercoder/solutions/_217.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* duplicates. Your function should return true if any value appears at least twice in the array,
1010
* and it should return false if every element is distinct.
1111
*/
12-
public class ContainsDuplicate {
12+
public class _217 {
1313
public boolean containsDuplicate(int[] nums) {
1414
if(nums == null || nums.length == 0) return false;
1515
Set<Integer> set = new HashSet();
@@ -21,7 +21,7 @@ public boolean containsDuplicate(int[] nums) {
2121

2222
public static void main(String...strings){
2323
int[] nums = new int[]{1,2,3,4, 3};
24-
ContainsDuplicate test = new ContainsDuplicate();
24+
_217 test = new _217();
2525
System.out.println(test.containsDuplicate(nums));
2626
}
2727
}

src/main/java/com/fishercoder/solutions/ShortestWordDistance.java renamed to src/main/java/com/fishercoder/solutions/_243.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
Note:
1212
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.*/
13-
public class ShortestWordDistance {
13+
public class _243 {
1414

1515
public int shortestDistance(String[] words, String word1, String word2) {
1616

0 commit comments

Comments
 (0)