Skip to content

Commit 811dbb9

Browse files
committed
Format
1 parent f815fcf commit 811dbb9

File tree

155 files changed

+4890
-5696
lines changed

Some content is hidden

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

155 files changed

+4890
-5696
lines changed

AddBinary.java

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
2-
31
/**
42
* Given two binary strings, return their sum (also a binary string).
5-
*
6-
* For example, a = "11" b = "1" Return "100".
3+
*
4+
* <p>For example, a = "11" b = "1" Return "100".
75
*/
8-
96
public class AddBinary {
10-
public String addBinary(String a, String b) {
11-
int i = a.length() - 1;
12-
int j = b.length() - 1;
13-
int da = 0;
14-
int db = 0;
15-
int adv = 0;
16-
StringBuffer result = new StringBuffer();
17-
while (i >= 0 && j >= 0) {
18-
da = a.charAt(i--) == '0' ? 0 : 1;
19-
db = b.charAt(j--) == '0' ? 0 : 1;
20-
int d = da + db + adv;
21-
result.append(d % 2 == 0 ? '0' : '1');
22-
adv = d >> 1;
23-
}
24-
if (i >= 0) {
25-
while (i >= 0) {
26-
da = a.charAt(i--) == '0' ? 0 : 1;
27-
int d = da + adv;
28-
result.append(d % 2 == 0 ? '0' : '1');
29-
adv = d >> 1;
30-
}
31-
} else if (j >= 0) {
32-
while (j >= 0) {
33-
db = b.charAt(j--) == '0' ? 0 : 1;
34-
int d = db + adv;
35-
result.append(d % 2 == 0 ? '0' : '1');
36-
adv = d >> 1;
37-
}
38-
}
39-
if (adv == 1) {
40-
result.append('1');
41-
}
42-
return result.reverse().toString();
43-
}
44-
}
7+
public String addBinary(String a, String b) {
8+
int i = a.length() - 1;
9+
int j = b.length() - 1;
10+
int da = 0;
11+
int db = 0;
12+
int adv = 0;
13+
StringBuffer result = new StringBuffer();
14+
while (i >= 0 && j >= 0) {
15+
da = a.charAt(i--) == '0' ? 0 : 1;
16+
db = b.charAt(j--) == '0' ? 0 : 1;
17+
int d = da + db + adv;
18+
result.append(d % 2 == 0 ? '0' : '1');
19+
adv = d >> 1;
20+
}
21+
if (i >= 0) {
22+
while (i >= 0) {
23+
da = a.charAt(i--) == '0' ? 0 : 1;
24+
int d = da + adv;
25+
result.append(d % 2 == 0 ? '0' : '1');
26+
adv = d >> 1;
27+
}
28+
} else if (j >= 0) {
29+
while (j >= 0) {
30+
db = b.charAt(j--) == '0' ? 0 : 1;
31+
int d = db + adv;
32+
result.append(d % 2 == 0 ? '0' : '1');
33+
adv = d >> 1;
34+
}
35+
}
36+
if (adv == 1) {
37+
result.append('1');
38+
}
39+
return result.reverse().toString();
40+
}
41+
}

AddTwoNumbers.java

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
1-
2-
31
/**
4-
* You are given two linked lists representing two non-negative numbers.
5-
*
6-
* The digits are stored in reverse order and each of their nodes contain a single digit.
7-
*
8-
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9-
*
10-
* Output: 7 -> 0 -> 8
2+
* You are given two linked lists representing two non-negative numbers.
3+
*
4+
* <p>The digits are stored in reverse order and each of their nodes contain a single digit.
5+
*
6+
* <p>Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
7+
*
8+
* <p>Output: 7 -> 0 -> 8
119
*/
12-
1310
public class AddTwoNumbers {
14-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
15-
if (l1 == null)
16-
return l2;
17-
if (l2 == null)
18-
return l1;
19-
ListNode head = new ListNode(0);
20-
ListNode cur = head;
21-
int plus = 0;
22-
while (l1 != null && l2 != null) {
23-
int sum = l1.val + l2.val + plus;
24-
plus = sum / 10;
25-
sum = sum % 10;
26-
cur.next = new ListNode(sum);
27-
cur = cur.next;
28-
l1 = l1.next;
29-
l2 = l2.next;
30-
}
31-
if (l1 != null) {
32-
if (plus != 0) {
33-
cur.next = addTwoNumbers(l1, new ListNode(plus));
34-
} else {
35-
cur.next = l1;
36-
}
37-
} else if (l2 != null) {
38-
if (plus != 0) {
39-
cur.next = addTwoNumbers(l2, new ListNode(plus));
40-
} else {
41-
cur.next = l2;
42-
}
43-
} else if (plus != 0) {
44-
cur.next = new ListNode(plus);
45-
}
11+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
12+
if (l1 == null) return l2;
13+
if (l2 == null) return l1;
14+
ListNode head = new ListNode(0);
15+
ListNode cur = head;
16+
int plus = 0;
17+
while (l1 != null && l2 != null) {
18+
int sum = l1.val + l2.val + plus;
19+
plus = sum / 10;
20+
sum = sum % 10;
21+
cur.next = new ListNode(sum);
22+
cur = cur.next;
23+
l1 = l1.next;
24+
l2 = l2.next;
25+
}
26+
if (l1 != null) {
27+
if (plus != 0) {
28+
cur.next = addTwoNumbers(l1, new ListNode(plus));
29+
} else {
30+
cur.next = l1;
31+
}
32+
} else if (l2 != null) {
33+
if (plus != 0) {
34+
cur.next = addTwoNumbers(l2, new ListNode(plus));
35+
} else {
36+
cur.next = l2;
37+
}
38+
} else if (plus != 0) {
39+
cur.next = new ListNode(plus);
40+
}
4641

47-
return head.next;
48-
}
49-
}
42+
return head.next;
43+
}
44+
}

Anagrams.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
2-
31
import java.util.ArrayList;
42
import java.util.Arrays;
53

64
/**
75
* Given an array of strings, return all groups of strings that are anagrams.
8-
*
9-
* Note: All inputs will be in lower-case.
6+
*
7+
* <p>Note: All inputs will be in lower-case.
108
*/
11-
129
public class Anagrams {
13-
public ArrayList<String> anagrams(String[] strs) {
14-
ArrayList<String> ret = new ArrayList<String>();
15-
ArrayList<String> ar = new ArrayList<String>();
16-
for (String s : strs) {
17-
char[] c = s.toCharArray();
18-
Arrays.sort(c);
19-
ar.add(new String(c));
20-
}
21-
int[] list = new int[strs.length];
22-
int tmp = 0;
23-
for (int i = 0; i < ar.size(); i++) {
24-
if (list[i] == 0) {
25-
list[i] = 1;
26-
tmp = 1;
27-
for (int j = i + 1; j < ar.size(); j++) {
28-
if (list[j] == 0 && ar.get(i).equals(ar.get(j))) {
29-
list[j] = 1;
30-
tmp++;
31-
}
32-
}
33-
if (tmp == 1) {
34-
list[i] = 0;
35-
}
36-
}
37-
}
38-
for (int i = 0; i < list.length; i++) {
39-
if (list[i] == 1)
40-
ret.add(strs[i]);
41-
}
42-
return ret;
43-
}
44-
}
10+
public ArrayList<String> anagrams(String[] strs) {
11+
ArrayList<String> ret = new ArrayList<String>();
12+
ArrayList<String> ar = new ArrayList<String>();
13+
for (String s : strs) {
14+
char[] c = s.toCharArray();
15+
Arrays.sort(c);
16+
ar.add(new String(c));
17+
}
18+
int[] list = new int[strs.length];
19+
int tmp = 0;
20+
for (int i = 0; i < ar.size(); i++) {
21+
if (list[i] == 0) {
22+
list[i] = 1;
23+
tmp = 1;
24+
for (int j = i + 1; j < ar.size(); j++) {
25+
if (list[j] == 0 && ar.get(i).equals(ar.get(j))) {
26+
list[j] = 1;
27+
tmp++;
28+
}
29+
}
30+
if (tmp == 1) {
31+
list[i] = 0;
32+
}
33+
}
34+
}
35+
for (int i = 0; i < list.length; i++) {
36+
if (list[i] == 1) ret.add(strs[i]);
37+
}
38+
return ret;
39+
}
40+
}

BalancedBinaryTree.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
21
/**
32
* Given a binary tree, determine if it is height-balanced.
4-
*
5-
* For this problem, a height-balanced binary tree is defined as a binary tree
6-
* in which the depth of the two subtrees of every node never differ by more
7-
* than 1.
8-
*
3+
*
4+
* <p>For this problem, a height-balanced binary tree is defined as a binary tree in which the depth
5+
* of the two subtrees of every node never differ by more than 1.
96
*/
10-
117
public class BalancedBinaryTree {
12-
public boolean isBalanced(TreeNode root) {
13-
return determine(root) >= 0 ? true : false;
14-
}
8+
public boolean isBalanced(TreeNode root) {
9+
return determine(root) >= 0 ? true : false;
10+
}
1511

16-
private int determine(TreeNode root) {
17-
if (root == null) {
18-
return 0;
19-
} else {
20-
int leftDepth = determine(root.left);
21-
int rightDepth = determine(root.right);
22-
if (leftDepth < 0 || rightDepth < 0
23-
|| Math.abs(leftDepth - rightDepth) > 1)
24-
return -1;
25-
return Math.max(leftDepth, rightDepth) + 1;
26-
}
27-
}
28-
}
12+
private int determine(TreeNode root) {
13+
if (root == null) {
14+
return 0;
15+
} else {
16+
int leftDepth = determine(root.left);
17+
int rightDepth = determine(root.right);
18+
if (leftDepth < 0 || rightDepth < 0 || Math.abs(leftDepth - rightDepth) > 1) return -1;
19+
return Math.max(leftDepth, rightDepth) + 1;
20+
}
21+
}
22+
}

BestTimetoBuyandSellStock.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
2-
31
/**
4-
* Say you have an array for which the ith element is the price of a given stock
5-
* on day i.
6-
*
7-
* If you were only permitted to complete at most one transaction (ie, buy one
8-
* and sell one share of the stock), design an algorithm to find the maximum
9-
* profit.
2+
* Say you have an array for which the ith element is the price of a given stock on day i.
3+
*
4+
* <p>If you were only permitted to complete at most one transaction (ie, buy one and sell one share
5+
* of the stock), design an algorithm to find the maximum profit.
106
*/
117
public class BestTimetoBuyandSellStock {
12-
public int maxProfit(int[] prices) {
13-
int lowest = 0;
14-
int maxProfit = 0;
15-
if (prices.length > 0) {
16-
lowest = prices[0];
17-
for (int i = 0; i < prices.length; i++) {
18-
if (lowest > prices[i]) {
19-
lowest = prices[i];
20-
}
21-
maxProfit = Math.max(maxProfit, prices[i] - lowest);
22-
}
23-
}
24-
return maxProfit;
25-
}
26-
}
8+
public int maxProfit(int[] prices) {
9+
int lowest = 0;
10+
int maxProfit = 0;
11+
if (prices.length > 0) {
12+
lowest = prices[0];
13+
for (int i = 0; i < prices.length; i++) {
14+
if (lowest > prices[i]) {
15+
lowest = prices[i];
16+
}
17+
maxProfit = Math.max(maxProfit, prices[i] - lowest);
18+
}
19+
}
20+
return maxProfit;
21+
}
22+
}

BestTimetoBuyandSellStockII.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
2-
31
/**
4-
* Say you have an array for which the ith element is the price of a given stock
5-
* on day i.
6-
*
7-
* Design an algorithm to find the maximum profit. You may complete as many
8-
* transactions as you like (ie, buy one and sell one share of the stock
9-
* multiple times). However, you may not engage in multiple transactions at the
10-
* same time (ie, you must sell the stock before you buy again).
2+
* Say you have an array for which the ith element is the price of a given stock on day i.
3+
*
4+
* <p>Design an algorithm to find the maximum profit. You may complete as many transactions as you
5+
* like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in
6+
* multiple transactions at the same time (ie, you must sell the stock before you buy again).
117
*/
12-
138
public class BestTimetoBuyandSellStockII {
14-
public int maxProfit(int[] prices) {
15-
int profit = 0;
16-
for (int i = 1; i < prices.length; i++) {
17-
int d = prices[i] - prices[i - 1];
18-
if (d > 0) {
19-
profit += d;
20-
}
21-
}
22-
return profit;
23-
}
24-
}
9+
public int maxProfit(int[] prices) {
10+
int profit = 0;
11+
for (int i = 1; i < prices.length; i++) {
12+
int d = prices[i] - prices[i - 1];
13+
if (d > 0) {
14+
profit += d;
15+
}
16+
}
17+
return profit;
18+
}
19+
}

0 commit comments

Comments
 (0)