Skip to content

Commit 991f3b3

Browse files
refactor for format
1 parent 4a1170b commit 991f3b3

File tree

13 files changed

+102
-41
lines changed

13 files changed

+102
-41
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ public List<String> findStrobogrammatic(int n) {
2222
}
2323

2424
private List<String> recursiveHelper(int n, int m) {
25-
if (n == 0) return new ArrayList<String>(Arrays.asList(""));
26-
if (n == 1) return new ArrayList<String>(Arrays.asList("0", "1", "8"));
25+
if (n == 0) {
26+
return new ArrayList<>(Arrays.asList(""));
27+
}
28+
if (n == 1) {
29+
return new ArrayList<>(Arrays.asList("0", "1", "8"));
30+
}
2731

2832
List<String> list = recursiveHelper(n - 2, m);
2933
List<String> res = new ArrayList<String>();

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public List<List<String>> groupStrings(String[] strings) {
3535
key += (word.charAt(i) - offset + 26) % 26;
3636
}
3737

38-
if (!map.containsKey(key)) map.put(key, new ArrayList<String>());
38+
if (!map.containsKey(key)) {
39+
map.put(key, new ArrayList<>());
40+
}
3941
map.get(key).add(word);
4042
}
4143

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ public boolean canAttendMeetings(Interval[] intervals) {
2424

2525
@Override
2626
public int compare(Interval o1, Interval o2) {
27-
if (o1.start > o2.start) return 1;
28-
else return -1;
27+
if (o1.start > o2.start) {
28+
return 1;
29+
} else {
30+
return -1;
31+
}
2932
}
3033

3134
});
3235

3336
for (int i = 0; i < list.size() - 1; i++) {
34-
if (list.get(i).end > list.get(i + 1).start) return false;
37+
if (list.get(i).end > list.get(i + 1).start) {
38+
return false;
39+
}
3540
}
3641
return true;
3742

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ public class _258 {
2222

2323
//only three cases as the code shows
2424
public int addDigits(int num) {
25-
if (num == 0) return 0;
26-
if (num % 9 == 0) return 9;
25+
if (num == 0) {
26+
return 0;
27+
}
28+
if (num % 9 == 0) {
29+
return 9;
30+
}
2731
return num % 9;
2832
}
2933

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ public int[] singleNumber(int[] nums) {
2727
int[] res = new int[2];
2828
int index = 0;
2929
for (int key : map.keySet()) {
30-
if (map.get(key) == 1) res[index++] = key;
31-
if (index == 2) break;
30+
if (map.get(key) == 1) {
31+
res[index++] = key;
32+
}
33+
if (index == 2) {
34+
break;
35+
}
3236
}
3337
return res;
3438
}

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ public boolean canPermutePalindrome(String s) {
2020
char[] chars = s.toCharArray();
2121
Map<Character, Integer> map = new HashMap<Character, Integer>();
2222
for (char c : chars) {
23-
if (!map.containsKey(c)) map.put(c, 1);
24-
else map.put(c, map.get(c) + 1);
23+
if (!map.containsKey(c)) {
24+
map.put(c, 1);
25+
} else {
26+
map.put(c, map.get(c) + 1);
27+
}
2528
}
2629
int evenCount = 0;
2730
for (Map.Entry<Character, Integer> e : map.entrySet()) {
28-
if (e.getValue() % 2 != 0) evenCount++;
29-
if (evenCount > 1) return false;
31+
if (e.getValue() % 2 != 0) {
32+
evenCount++;
33+
}
34+
if (evenCount > 1) {
35+
return false;
36+
}
3037
}
3138
return true;
3239

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@ public List<String> generatePalindromes(String s) {
3636
}
3737

3838
// cannot form any palindromic string
39-
if (odd > 1)
39+
if (odd > 1) {
4040
return res;
41+
}
4142

4243
// step 2. add half count of each character to list
4344
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
4445
char key = entry.getKey();
4546
int val = entry.getValue();
4647

47-
if (val % 2 != 0)
48+
if (val % 2 != 0) {
4849
mid += key;
50+
}
4951

50-
for (int i = 0; i < val / 2; i++)
52+
for (int i = 0; i < val / 2; i++) {
5153
list.add(key);
54+
}
5255
}
5356

5457
// step 3. generate all the permutations
@@ -69,8 +72,9 @@ void getPerm(List<Character> list, String mid, boolean[] used, StringBuilder sb,
6972

7073
for (int i = 0; i < list.size(); i++) {
7174
// avoid duplication
72-
if (i > 0 && list.get(i) == list.get(i - 1) && !used[i - 1])
75+
if (i > 0 && list.get(i) == list.get(i - 1) && !used[i - 1]) {
7376
continue;
77+
}
7478

7579
if (!used[i]) {
7680
used[i] = true;

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public static String alienOrder(String[] words) {
3535
Map<Character, Set<Character>> map = new HashMap();
3636
Map<Character, Integer> degree = new HashMap<>();
3737
String result = "";
38-
if (words == null || words.length == 0) return result;
38+
if (words == null || words.length == 0) {
39+
return result;
40+
}
3941
for (String s : words) {
4042
for (char c : s.toCharArray()) {
4143
degree.put(c, 0);//keeps overwriting it, the purpose is to create one entry
@@ -65,7 +67,9 @@ public static String alienOrder(String[] words) {
6567
}
6668
Queue<Character> queue = new LinkedList<>();
6769
for (char c : degree.keySet()) {
68-
if (degree.get(c) == 0) queue.add(c);
70+
if (degree.get(c) == 0) {
71+
queue.add(c);
72+
}
6973
}
7074
while (!queue.isEmpty()) {
7175
char c = queue.remove();
@@ -79,7 +83,9 @@ public static String alienOrder(String[] words) {
7983
}
8084
}
8185
}
82-
if (result.length() != degree.size()) return "";
86+
if (result.length() != degree.size()) {
87+
return "";
88+
}
8389
return result;
8490
}
8591

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public int removeElement_editorial_solution_1(int[] nums, int val) {
1818
//use two pointers, increment j as long as its not equal to val, return i in the end
1919
int i = 0;
2020
for (int j = 0; j < nums.length; j++) {
21-
if (nums[j] != val) nums[i++] = nums[j];
21+
if (nums[j] != val) {
22+
nums[i++] = nums[j];
23+
}
2224
}
2325
return i;
2426
}
@@ -49,7 +51,9 @@ public int removeElement(int[] nums, int val) {
4951
throwPosition--;
5052
count++;
5153
}
52-
if (throwPosition == -1 || i >= throwPosition) break;
54+
if (throwPosition == -1 || i >= throwPosition) {
55+
break;
56+
}
5357
if (nums[i] == val) {
5458
count++;
5559
int temp = nums[throwPosition];

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public class _270 {
1212
class GeneralTreeSolution {
1313
//this finished in 1 ms
1414
public int closestValue(TreeNode root, double target) {
15-
if (root == null)
15+
if (root == null) {
1616
return 0;
17+
}
1718
double delta = Double.MAX_VALUE;
1819
return dfs(root, target, delta, root.val);
1920
}
@@ -24,11 +25,13 @@ private int dfs(TreeNode root, double target, double delta, int closestVal) {
2425
delta = Math.abs(root.val - target);
2526
}
2627
int leftVal = closestVal;
27-
if (root.left != null)
28+
if (root.left != null) {
2829
leftVal = dfs(root.left, target, delta, closestVal);
30+
}
2931
int rightVal = closestVal;
30-
if (root.right != null)
32+
if (root.right != null) {
3133
rightVal = dfs(root.right, target, delta, closestVal);
34+
}
3235
return (Math.abs(leftVal - target) > Math.abs(rightVal - target)) ? rightVal : leftVal;
3336
}
3437
}
@@ -37,12 +40,16 @@ class BSTSolutionRecursive {
3740
//we can tailor the solution to use the BST feature: left subtrees are always smaller than the root the right subtrees
3841
//this finished in 0 ms
3942
public int closestValue(TreeNode root, double target) {
40-
if (root == null) return 0;
43+
if (root == null) {
44+
return 0;
45+
}
4146
return dfs(root, target, root.val);
4247
}
4348

4449
private int dfs(TreeNode root, double target, int minVal) {
45-
if (root == null) return minVal;
50+
if (root == null) {
51+
return minVal;
52+
}
4653
if (Math.abs(root.val - target) < Math.abs(minVal - target)) {
4754
minVal = root.val;
4855
}
@@ -57,13 +64,16 @@ private int dfs(TreeNode root, double target, int minVal) {
5764

5865
class GeneralTreeSolutionMoreConcise {
5966
public int closestValue(TreeNode root, double target) {
60-
if (root == null) return 0;
67+
if (root == null) {
68+
return 0;
69+
}
6170
return dfs(root, target, root.val);
6271
}
6372

6473
private int dfs(TreeNode root, double target, int minVal) {
65-
if (root == null)
74+
if (root == null) {
6675
return minVal;
76+
}
6777
if (Math.abs(root.val - target) < Math.abs(minVal - target)) {
6878
minVal = root.val;
6979
}
@@ -80,8 +90,11 @@ public int closestValue(TreeNode root, double target) {
8090
if (Math.abs(root.val - target) < Math.abs(minVal - target)) {
8191
minVal = root.val;
8292
}
83-
if (target < root.val) root = root.left;
84-
else root = root.right;
93+
if (target < root.val) {
94+
root = root.left;
95+
} else {
96+
root = root.right;
97+
}
8598
}
8699
return minVal == Long.MAX_VALUE ? 0 : (int) minVal;
87100
}

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,31 @@ public List<Integer> closestKValues(TreeNode root, double target, int k) {
3838
inorder(root, target, true, s2);
3939

4040
while (k-- > 0) {
41-
if (s1.isEmpty())
41+
if (s1.isEmpty()) {
4242
res.add(s2.pop());
43-
else if (s2.isEmpty())
43+
} else if (s2.isEmpty()) {
4444
res.add(s1.pop());
45-
else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target))
45+
} else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target)) {
4646
res.add(s1.pop());
47-
else
47+
} else {
4848
res.add(s2.pop());
49+
}
4950
}
5051

5152
return res;
5253
}
5354

5455
// inorder traversal
5556
void inorder(TreeNode root, double target, boolean reverse, Stack<Integer> stack) {
56-
if (root == null)
57+
if (root == null) {
5758
return;
59+
}
5860

5961
inorder(reverse ? root.right : root.left, target, reverse, stack);
6062
// early terminate, no need to traverse the whole tree
61-
if ((reverse && root.val <= target) || (!reverse && root.val > target))
63+
if ((reverse && root.val <= target) || (!reverse && root.val > target)) {
6264
return;
65+
}
6366
// track the value of current node
6467
stack.push(root.val);
6568
inorder(reverse ? root.left : root.right, target, reverse, stack);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public class _273 {
2222

2323
public String numberToWords(int num) {
2424
String result;
25-
if (num == 0) return belowTen[num];
25+
if (num == 0) {
26+
return belowTen[num];
27+
}
2628

2729
result = hundredHelper(num % 1000);
2830
num = num / 1000;

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
public class _276 {
1313
public int numWays(int n, int k) {
14-
if (n == 0) return 0;
15-
else if (n == 1) return k;
14+
if (n == 0) {
15+
return 0;
16+
} else if (n == 1) {
17+
return k;
18+
}
1619
int sameColorCnt = k;
1720
int diffColorCnt = k * (k - 1);
1821
for (int i = 2; i < n; i++) {

0 commit comments

Comments
 (0)