Skip to content

Commit 088038a

Browse files
committed
Format
1 parent 213ef5e commit 088038a

File tree

81 files changed

+1341
-1377
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1341
-1377
lines changed

3sum.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ? b ? c)
66
The solution set must not contain duplicate triplets.
7-
For example, given array S = {-1 0 1 2 -1 -4},
87

9-
A solution set is:
10-
(-1, 0, 1)
11-
(-1, -1, 2)
8+
For example, given array S = {-1 0 1 2 -1 -4},
9+
10+
A solution set is:
11+
(-1, 0, 1)
12+
(-1, -1, 2)
1213

1314
public class Solution {
1415
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {

4Sum.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ? b ? c ? d)
66
The solution set must not contain duplicate quadruplets.
7-
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
87

9-
A solution set is:
10-
(-1, 0, 0, 1)
11-
(-2, -1, 1, 2)
12-
(-2, 0, 0, 2)
8+
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
9+
10+
A solution set is:
11+
(-1, 0, 0, 1)
12+
(-2, -1, 1, 2)
13+
(-2, 0, 0, 2)
1314

1415
public class Solution {
1516
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {

Add Binary.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@ Given two binary strings, return their sum (also a binary string).
88
public class Solution {
99
public String addBinary(String a, String b) {
1010
int i = a.length() - 1;
11-
int j = b.length() - 1;
12-
int da = 0;
13-
int db = 0;
14-
int adv = 0;
15-
StringBuffer result = new StringBuffer();
16-
while (i >= 0 && j >= 0) {
17-
da = a.charAt(i--) == '0' ? 0 : 1;
18-
db = b.charAt(j--) == '0' ? 0 : 1;
19-
int d = da + db + adv;
20-
result.append(d % 2 == 0 ? '0' : '1');
21-
adv = d >> 1;
22-
}
23-
if (i >= 0) {
24-
while (i >= 0) {
25-
da = a.charAt(i--) == '0' ? 0 : 1;
26-
int d = da + adv;
27-
result.append(d % 2 == 0 ? '0' : '1');
28-
adv = d >> 1;
29-
}
30-
} else if (j >= 0) {
31-
while (j >= 0) {
32-
db = b.charAt(j--) == '0' ? 0 : 1;
33-
int d = db + adv;
34-
result.append(d % 2 == 0 ? '0' : '1');
35-
adv = d >> 1;
36-
}
37-
}
38-
if (adv == 1) {
39-
result.append('1');
40-
}
41-
return result.reverse().toString();
11+
int j = b.length() - 1;
12+
int da = 0;
13+
int db = 0;
14+
int adv = 0;
15+
StringBuffer result = new StringBuffer();
16+
while (i >= 0 && j >= 0) {
17+
da = a.charAt(i--) == '0' ? 0 : 1;
18+
db = b.charAt(j--) == '0' ? 0 : 1;
19+
int d = da + db + adv;
20+
result.append(d % 2 == 0 ? '0' : '1');
21+
adv = d >> 1;
4222
}
23+
if (i >= 0) {
24+
while (i >= 0) {
25+
da = a.charAt(i--) == '0' ? 0 : 1;
26+
int d = da + adv;
27+
result.append(d % 2 == 0 ? '0' : '1');
28+
adv = d >> 1;
29+
}
30+
} else if (j >= 0) {
31+
while (j >= 0) {
32+
db = b.charAt(j--) == '0' ? 0 : 1;
33+
int d = db + adv;
34+
result.append(d % 2 == 0 ? '0' : '1');
35+
adv = d >> 1;
36+
}
37+
}
38+
if (adv == 1) {
39+
result.append('1');
40+
}
41+
return result.reverse().toString();
42+
}
4343
}

Anagrams.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
public class Solution {
66
public ArrayList<String> anagrams(String[] strs) {
77
ArrayList<String> ret = new ArrayList<String>();
8-
ArrayList<String> ar = new ArrayList<String>();
9-
for (String s : strs) {
10-
char[] c = s.toCharArray();
11-
Arrays.sort(c);
12-
ar.add(new String(c));
8+
ArrayList<String> ar = new ArrayList<String>();
9+
for (String s : strs) {
10+
char[] c = s.toCharArray();
11+
Arrays.sort(c);
12+
ar.add(new String(c));
13+
}
14+
int[] list = new int[strs.length];
15+
int tmp = 0;
16+
for (int i = 0; i < ar.size(); i++) {
17+
if (list[i] == 0) {
18+
list[i] = 1;
19+
tmp = 1;
20+
for (int j = i + 1; j < ar.size(); j++) {
21+
if (list[j] == 0 && ar.get(i).equals(ar.get(j))) {
22+
list[j] = 1;
23+
tmp++;
24+
}
1325
}
14-
int[] list = new int[strs.length];
15-
int tmp = 0;
16-
for (int i = 0; i < ar.size(); i++) {
17-
if (list[i] == 0) {
18-
list[i] = 1;
19-
tmp = 1;
20-
for (int j = i + 1; j < ar.size(); j++) {
21-
if (list[j] == 0 && ar.get(i).equals(ar.get(j))) {
22-
list[j] = 1;
23-
tmp++;
24-
}
25-
}
26-
if (tmp == 1) {
27-
list[i] = 0;
28-
}
29-
}
26+
if (tmp == 1) {
27+
list[i] = 0;
3028
}
31-
for (int i = 0; i < list.length; i++) {
32-
if (list[i] == 1)
33-
ret.add(strs[i]);
34-
}
35-
return ret;
29+
}
30+
}
31+
for (int i = 0; i < list.length; i++) {
32+
if (list[i] == 1)
33+
ret.add(strs[i]);
34+
}
35+
return ret;
3636
}
3737
}

Balanced Binary Tree.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
public class Solution {
1616
public boolean isBalanced(TreeNode root) {
1717
return determine(root) >= 0 ? true : false;
18-
}
18+
}
1919

20-
public int determine(TreeNode root) {
21-
if (root == null) {
22-
return 0;
23-
} else {
24-
int leftDepth = determine(root.left);
25-
int rightDepth = determine(root.right);
26-
if (leftDepth < 0 || rightDepth < 0 || Math.abs(leftDepth - rightDepth) > 1) return -1;
27-
return Math.max(leftDepth, rightDepth) + 1;
28-
}
20+
public int determine(TreeNode root) {
21+
if (root == null) {
22+
return 0;
23+
} else {
24+
int leftDepth = determine(root.left);
25+
int rightDepth = determine(root.right);
26+
if (leftDepth < 0 || rightDepth < 0 || Math.abs(leftDepth - rightDepth) > 1) return -1;
27+
return Math.max(leftDepth, rightDepth) + 1;
2928
}
29+
}
3030
}

Best Time to Buy and Sell Stock III.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
77

88
public class Solution {
9-
public int maxProfit(int[] prices) {
10-
if (prices.length < 2) return 0;
11-
int max = 0;
12-
for (int i = 0; i < prices.length; i++) {
13-
max = Math.max(max, maxProfitSingle(prices, 0, i) + maxProfitSingle(prices, i, prices.length - 1));
14-
}
15-
return max;
9+
public int maxProfit(int[] prices) {
10+
if (prices.length < 2) return 0;
11+
int max = 0;
12+
for (int i = 0; i < prices.length; i++) {
13+
max = Math.max(max, maxProfitSingle(prices, 0, i) + maxProfitSingle(prices, i, prices.length - 1));
14+
}
15+
return max;
1616
}
1717

18-
public int maxProfitSingle(int[] prices, int start, int end) {
18+
public int maxProfitSingle(int[] prices, int start, int end) {
1919
int lowest = 0;
2020
int maxProfit = 0;
2121
if (end - start + 1 > 0) {

Binary Tree Inorder Traversal.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ public ArrayList<Integer> inorderTraversal(TreeNode root) {
2828
s.add(root);
2929
TreeNode p = root.left;
3030
while (!s.empty()) {
31-
while (p != null) {
32-
s.add(p);
33-
p = p.left;
34-
}
35-
TreeNode n = s.pop();
36-
inOrder.add(n.val);
37-
p = n.right;
38-
if (p != null) {
39-
s.add(p);
40-
p = p.left;
41-
}
31+
while (p != null) {
32+
s.add(p);
33+
p = p.left;
34+
}
35+
TreeNode n = s.pop();
36+
inOrder.add(n.val);
37+
p = n.right;
38+
if (p != null) {
39+
s.add(p);
40+
p = p.left;
41+
}
4242
}
4343
return inOrder;
4444
}

Binary Tree Level Order Traversal II.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
3535
ArrayList<Integer> level = new ArrayList<Integer>();
3636
q.add(root);
3737
while(!q.isEmpty()) {
38-
TreeNode n = q.poll();
39-
curr--;
40-
level.add(n.val);
41-
if (n.left != null) {
42-
q.add(n.left);
43-
next++;
44-
}
45-
if (n.right != null) {
46-
q.add(n.right);
47-
next++;
48-
}
49-
if (curr == 0) {
50-
levels.add(level);
51-
level = new ArrayList<Integer>();
52-
curr = next;
53-
next = 0;
54-
}
38+
TreeNode n = q.poll();
39+
curr--;
40+
level.add(n.val);
41+
if (n.left != null) {
42+
q.add(n.left);
43+
next++;
44+
}
45+
if (n.right != null) {
46+
q.add(n.right);
47+
next++;
48+
}
49+
if (curr == 0) {
50+
levels.add(level);
51+
level = new ArrayList<Integer>();
52+
curr = next;
53+
next = 0;
54+
}
5555
}
5656
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
5757
for (int i = levels.size() - 1; i >= 0; i--) {
58-
result.add(levels.get(i));
58+
result.add(levels.get(i));
5959
}
6060
return result;
6161
}

Binary Tree Maximum Path Sum.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,38 @@
1212

1313
public class Solution {
1414

15-
public class MyInt {
15+
public class MyInt {
1616

17-
private int value;
17+
private int value;
1818

19-
public int getValue() {
20-
return value;
21-
}
22-
23-
public void setValue(int value) {
24-
this.value = value;
25-
}
19+
public int getValue() {
20+
return value;
21+
}
2622

27-
public MyInt(int value) {
28-
this.value = value;
29-
}
23+
public void setValue(int value) {
24+
this.value = value;
25+
}
3026

27+
public MyInt(int value) {
28+
this.value = value;
3129
}
30+
31+
}
3232

33-
public int maxPathSum(TreeNode root) {
34-
MyInt maxSum = new MyInt(Integer.MIN_VALUE);
35-
findMaxSum(root, maxSum);
36-
return maxSum.getValue();
37-
}
33+
public int maxPathSum(TreeNode root) {
34+
MyInt maxSum = new MyInt(Integer.MIN_VALUE);
35+
findMaxSum(root, maxSum);
36+
return maxSum.getValue();
37+
}
3838

39-
public int findMaxSum(TreeNode root, MyInt maxSum) {
39+
public int findMaxSum(TreeNode root, MyInt maxSum) {
4040
if (root == null)
41-
return 0;
42-
int cpath = 0;
43-
int left = findMaxSum(root.left, maxSum);
44-
int right = findMaxSum(root.right, maxSum);
45-
cpath = Math.max(root.val, Math.max(root.val + left, root.val + right));
46-
maxSum.setValue(Math.max(maxSum.getValue(), Math.max(root.val + left + right, cpath)));
47-
return cpath;
48-
}
41+
return 0;
42+
int cpath = 0;
43+
int left = findMaxSum(root.left, maxSum);
44+
int right = findMaxSum(root.right, maxSum);
45+
cpath = Math.max(root.val, Math.max(root.val + left, root.val + right));
46+
maxSum.setValue(Math.max(maxSum.getValue(), Math.max(root.val + left + right, cpath)));
47+
return cpath;
48+
}
4949
}

Climbing Stairs.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
44

5-
public int climbStairs(int n) {
6-
int[] arr = new int[n + 1];
7-
arr[0] = 1;
8-
arr[1] = 1;
9-
for (int i = 2; i <= n; i++) {
10-
arr[i] = arr[i - 1] + arr[i - 2];
11-
}
12-
return arr[n];
13-
}
5+
public int climbStairs(int n) {
6+
int[] arr = new int[n + 1];
7+
arr[0] = 1;
8+
arr[1] = 1;
9+
for (int i = 2; i <= n; i++) {
10+
arr[i] = arr[i - 1] + arr[i - 2];
11+
}
12+
return arr[n];
13+
}

0 commit comments

Comments
 (0)