8000 refactor for format · IsaacVictor/Leetcode@a1d5dc2 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit a1d5dc2

Browse files
refactor for format
1 parent 991f3b3 commit a1d5dc2

Some content is hidden

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

45 files changed

+324
-120
lines changed

src/main/java/com/fishercoder/solutions/_2.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3434
;
3535
tmp = tmp.next;
3636
}
37-
if (sum / 10 == 1)
37+
if (sum / 10 == 1) {
3838
tmp.next = new ListNode(1);//this means there's a carry, so we add additional 1, e.g. [5] + [5] = [0, 1]
39+
}
3940
return result.val == 0 ? result.next : result;
4041
}
4142
}

src/main/java/com/fishercoder/solutions/_20.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ public boolean isValid(String s) {
1616
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
1717
stack.push(s.charAt(i));
1818
} else {
19-
if (stack.isEmpty()) return false;
20-
else {
21-
if (stack.peek() == '(' && s.charAt(i) != ')') return false;
22-
else if (stack.peek() == '{' && s.charAt(i) != '}') return false;
23-
else if (stack.peek() == '[' && s.charAt(i) != ']') return false;
19+
if (stack.isEmpty()) {
20+
return false;
21+
} else {
22+
if (stack.peek() == '(' && s.charAt(i) != ')') {
23+
return false;
24+
} else if (stack.peek() == '{' && s.charAt(i) != '}') {
25+
return false;
26+
} else if (stack.peek() == '[' && s.charAt(i) != ']') {
27+
return false;
28+
}
2429
stack.pop();
2530
}
2631
}

src/main/java/com/fishercoder/solutions/_200.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public class _200 {
2929
public static class DFSSolution {
3030

3131
public int numIslands(char[][] grid) {
32-
if (grid == null || grid.length == 0) return 0;
32+
if (grid == null || grid.length == 0) {
33+
return 0;
34+
}
3335
int count = 0;
3436
int m = grid.length;
3537
int n = grid[0].length;
@@ -45,7 +47,9 @@ public int numIslands(char[][] grid) {
4547
}
4648

4749
void dfs(char[][] grid, int i, int j, int m, int n) {
48-
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') return;
50+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') {
51+
return;
52+
}
4953
grid[i][j] = '0';
5054
dfs(grid, i + 1, j, m, n);
5155
dfs(grid, i, j + 1, m, n);
@@ -87,13 +91,17 @@ public void union(int i, int j) {
8791
}
8892

8993
public int find(int[] ids, int i) {
90-
if (ids[i] == i) return i;
94+
if (ids[i] == i) {
95+
return i;
96+
}
9197
return find(ids, ids[i]);
9298
}
9399
}
94100

95101
public int numIslands(char[][] grid) {
96-
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
102+
if (grid == null || grid.length == 0 || grid[0].length == 0) {
103+
return 0;
104+
}
97105
int[] dirs = new int[]{0, 1, 0, -1, 0};
98106
UnionFind uf = new UnionFind(grid);
99107
int m = grid.length;

src/main/java/com/fishercoder/solutions/_201.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class _201 {
1212

1313
//this naive approach works, but will result in TLE as expected for 8256/8266 test cases: (0, 2147483647)
1414
public int rangeBitwiseAnd_TLE(int m, int n) {
15-
if (m == 0) return m;
15+
if (m == 0) {
16+
return m;
17+
}
1618
int result = m;
1719
for (int i = m + 1; i <= n; i++) {
1820
result &= i;

src/main/java/com/fishercoder/solutions/_202.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
public class _202 {
2020

2121
public static boolean isHappy(int n) {
22-
if (n == 1) return true;
22+
if (n == 1) {
23+
return true;
24+
}
2325
Set<Integer> set = new HashSet();
2426
while (n != 1) {
2527
String str = String.valueOf(n);
@@ -28,8 +30,12 @@ public static boolean isHappy(int n) {
2830
int temp = Character.getNumericValue(str.charAt(i));
2931
n += temp * temp;
3032
}
31-
if (n == 1) return true;
32-
if (!set.add(n)) return false;
33+
if (n == 1) {
34+
return true;
35+
}
36+
if (!set.add(n)) {
37+
return false;
38+
}
3339
}
3440
return false;
3541
}

src/main/java/com/fishercoder/solutions/_205.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@ public class _205 {
2121
/**space should be O(1) since it only has alphabetic letters which are capped at 52.*/
2222

2323
public boolean isIsomorphic(String s, String t) {
24-
if (s == null || s.length() == 0) return (t == null || t.length() == 0);
25-
if (t == null || t.length() == 0) return (s == null || s.length() == 0);
24+
if (s == null || s.length() == 0) {
25+
return (t == null || t.length() == 0);
26+
}
27+
if (t == null || t.length() == 0) {
28+
return (s == null || s.length() == 0);
29+
}
2630
char[] schar = s.toCharArray();
2731
char[] tchar = t.toCharArray();
2832
Map<Character, Character> map = new HashMap();
29-
if (s.length() != t.length()) return false;
33+
if (s.length() != t.length()) {
34+
return false;
35+
}
3036
for (int i = 0; i < s.length(); i++) {
3137
if (map.containsKey(schar[i])) {
32-
if (map.get(schar[i]) != tchar[i]) return false;
38+
if (map.get(schar[i]) != tchar[i]) {
39+
return false;
40+
}
3341
} else {
34-
if (map.containsValue(tchar[i])) return false;//this line is necessary for this case: ("ab", "aa")
42+
if (map.containsValue(tchar[i])) {
43+
return false;//this line is necessary for this case: ("ab", "aa")
44+
}
3545
map.put(schar[i], tchar[i]);
3646
}
3747
}

src/main/java/com/fishercoder/solutions/_206.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public ListNode reverseList_recursive(ListNode head) {
4040
}
4141

4242
ListNode reverse(ListNode head, ListNode newHead) {
43-
if (head == null) return newHead;
43+
if (head == null) {
44+
return newHead;
45+
}
4446
ListNode next = head.next;
4547
head.next = newHead;
4648
newHead = head;

src/main/java/com/fishercoder/solutions/_207.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ public boolean canFinish(int numCourses, int[][] prerequisites) {
3737
}
3838
Set<Integer> zeroDegree = new HashSet();
3939
for (int i = 0; i < numCourses; i++) {
40-
if (indegree[i] == 0) zeroDegree.add(i);
40+
if (indegree[i] == 0) {
41+
zeroDegree.add(i);
42+
}
43+
}
44+
if (zeroDegree.isEmpty()) {
45+
return false;
4146
}
42-
if (zeroDegree.isEmpty()) return false;
4347

4448
while (!zeroDegree.isEmpty()) {
4549
Iterator<Integer> it = zeroDegree.iterator();
@@ -48,13 +52,17 @@ public boolean canFinish(int numCourses, int[][] prerequisites) {
4852
for (int[] prereq : prerequisites) {
4953
if (prereq[1] == course) {
5054
indegree[prereq[0]]--;
51-
if (indegree[prereq[0]] == 0) zeroDegree.add(prereq[0]);
55+
if (indegree[prereq[0]] == 0) {
56+
zeroDegree.add(prereq[0]);
57+
}
5258
}
5359
}
5460
}
5561

5662
for (int i : indegree) {
57-
if (i != 0) return false;
63+
if (i != 0) {
64+
return false;
65+
}
5866
}
5967
return true;
6068
}

src/main/java/com/fishercoder/solutions/_208.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public void insert(String word) {
4949
public boolean search(String word) {
5050
TrieNode node = root;
5151
for (int i = 0; i < word.length(); i++) {
52-
if (node.children[word.charAt(i) - 'a'] == null) return false;
52+
if (node.children[word.charAt(i) - 'a'] == null) {
53+
return false;
54+
}
5355
node = node.children[word.charAt(i) - 'a'];
5456
}
5557
return node.isWord;
@@ -60,7 +62,9 @@ public boolean search(String word) {
6062
public boolean startsWith(String prefix) {
6163
TrieNode node = root;
6264
for (int i = 0; i < prefix.length(); i++) {
63-
if (node.children[prefix.charAt(i) - 'a'] == null) return false;
65+
if (node.children[prefix.charAt(i) - 'a'] == null) {
66+
return false;
67+
}
6468
node = node.children[prefix.charAt(i) - 'a'];
6569
}
6670
return true;

src/main/java/com/fishercoder/solutions/_209.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ If you have figured out the O(n) solution, try coding another solution of which
1414
public class _209 {
1515

1616
public int minSubArrayLen(int s, int[] nums) {
17-
if (nums == null || nums.length == 0) return 0;
17+
if (nums == null || nums.length == 0) {
18+
return 0;
19+
}
1820
int i = 0;
1921
int j = 0;
2022
int min = Integer.MAX_VALUE;

0 commit comments

Comments
 (0)
0