Skip to content

Commit ed300ca

Browse files
refactor for format
1 parent f10f545 commit ed300ca

37 files changed

+325
-321
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@ public class _191 {
88
//another cool trick that I learned: doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n
99
//to zero, here's the solution from Editorial:
1010
//example run for the above editorial solution: input 5, n will be 5&4 and becomes 4, then in the next run, n will become 4&3 which is 0, thus exit the while loop.
11-
public int hammingWeight_editorial(int n){
11+
public int hammingWeight_editorial(int n) {
1212
int count = 0;
13-
while(n != 0){
13+
while (n != 0) {
1414
count++;
15-
n &= (n-1);
15+
n &= (n - 1);
1616
}
1717
return count;
1818
}
1919

20-
public static void main(String...strings){
21-
System.out.println(4&5);
20+
public static void main(String... strings) {
21+
System.out.println(4 & 5);
2222
_191 test = new _191();
2323
System.out.println(test.hammingWeight_editorial(5));
2424
}
2525

26-
// you need to treat n as an unsigned value
26+
// you need to treat n as an unsigned value
2727
public int hammingWeight(int n) {
2828
//cheers! Made it AC'ed on my own with an ease!
2929
int count = 0;
30-
for(int i = 0; i < 32; i++){
30+
for (int i = 0; i < 32; i++) {
3131
int one = (n >>> i) & 1;//must use unsigned right shift operator
32-
if(one == 1) count++;
32+
if (one == 1) count++;
3333
}
3434
return count;
3535
}
36-
36+
3737
//then I turned to its Editorial solution: we can make it a little faster: at any time, when n becomes zero, that means there's
3838
//no more 1's there, then we could safely return! Cool!
3939
public int hammingWeight_faster(int n) {
4040
int count = 0;
41-
for(int i = 0; i < 32; i++){
41+
for (int i = 0; i < 32; i++) {
4242
int one = (n >>> i) & 1;//must use unsigned right shift operator
43-
if(one == 1) count++;
44-
if(n == 0) return count;
43+
if (one == 1) count++;
44+
if (n == 0) return count;
4545
}
4646
return count;
4747
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
public class _198 {
1414

1515
public int rob(int[] nums) {
16-
if(nums == null || nums.length == 0) return 0;
17-
if(nums.length == 1) return nums[0];
18-
if(nums.length == 2) return Math.max(nums[0], nums[1]);
16+
if (nums == null || nums.length == 0) return 0;
17+
if (nums.length == 1) return nums[0];
18+
if (nums.length == 2) return Math.max(nums[0], nums[1]);
1919
int[] dp = new int[nums.length];
2020
dp[0] = nums[0];
2121
dp[1] = Math.max(nums[0], nums[1]);
22-
for(int i = 2; i < nums.length; i++){
23-
dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]);
22+
for (int i = 2; i < nums.length; i++) {
23+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
2424
}
25-
return dp[nums.length-1];
25+
return dp[nums.length - 1];
2626
}
2727

2828
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ public class _25 {
2424
public ListNode reverseKGroup(ListNode head, int k) {
2525
ListNode curr = head;
2626
int count = 0;
27-
while(curr != null && count != k){//find the k+1 node
27+
while (curr != null && count != k) {
28+
//find the k+1 node
2829
curr = curr.next;
2930
count++;
3031
}
3132

32-
if(count == k){//if k+1 is found
33+
if (count == k) {
34+
//if k+1 is found
3335
curr = reverseKGroup(curr, k);//reverse list that has k+1 as head
3436

35-
while(count > 0){
37+
while (count > 0) {
3638
ListNode temp = head.next;
3739
head.next = curr;
3840
curr = head;
@@ -44,7 +46,7 @@ public ListNode reverseKGroup(ListNode head, int k) {
4446
return head;//we run out of nodes before we hit count == k, so we'll just directly return head in this case as well
4547
}
4648

47-
public static void main(String...args) {
49+
public static void main(String... args) {
4850
ListNode head = new ListNode(1);
4951
head.next = new ListNode(2);
5052
head.next.next = new ListNode(3);

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@
1414
return false.*/
1515
public class _252 {
1616
public boolean canAttendMeetings(Interval[] intervals) {
17-
17+
1818
List<Interval> list = new ArrayList();
19-
for(Interval interval : intervals){
19+
for (Interval interval : intervals) {
2020
list.add(interval);
2121
}
22-
23-
Collections.sort(list, new Comparator<Interval>(){
22+
23+
Collections.sort(list, new Comparator<Interval>() {
2424

2525
@Override
2626
public int compare(Interval o1, Interval o2) {
27-
if(o1.start > o2.start) return 1;
27+
if (o1.start > o2.start) return 1;
2828
else return -1;
2929
}
30-
30+
3131
});
32-
33-
for(int i = 0; i < list.size()-1; i++){
34-
if(list.get(i).end > list.get(i+1).start) return false;
32+
33+
for (int i = 0; i < list.size() - 1; i++) {
34+
if (list.get(i).end > list.get(i + 1).start) return false;
3535
}
3636
return true;
37-
37+
3838
}
3939
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
public class _256 {
1212

1313
public int minCost(int[][] costs) {
14-
if(costs == null || costs.length == 0){
14+
if (costs == null || costs.length == 0) {
1515
return 0;
1616
}
17-
for(int i = 1; i < costs.length; i++){
18-
costs[i][0] += Math.min(costs[i-1][1], costs[i-1][2]);
19-
costs[i][1] += Math.min(costs[i-1][0], costs[i-1][2]);
20-
costs[i][2] += Math.min(costs[i-1][1], costs[i-1][0]);
17+
for (int i = 1; i < costs.length; i++) {
18+
costs[i][0] += Math.min(costs[i - 1][1], costs[i - 1][2]);
19+
costs[i][1] += Math.min(costs[i - 1][0], costs[i - 1][2]);
20+
costs[i][2] += Math.min(costs[i - 1][1], costs[i - 1][0]);
2121
}
22-
int n = costs.length-1;
22+
int n = costs.length - 1;
2323
return Math.min(Math.min(costs[n][0], costs[n][1]), costs[n][2]);
2424
}
2525

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ 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;
27-
return num%9;
25+
if (num == 0) return 0;
26+
if (num % 9 == 0) return 9;
27+
return num % 9;
2828
}
2929

3030
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class _259 {
1919

2020
/**Basically, very similar to 3Sum, but the key is that you'll have to add result by (right-left), not just increment result by 1!*/
2121
public int threeSumSmaller(int[] nums, int target) {
22-
if(nums == null || nums.length == 0) return 0;
22+
if (nums == null || nums.length == 0) return 0;
2323
int result = 0;
2424
Arrays.sort(nums);
25-
for(int i = 0; i < nums.length-2; i++){
26-
int left = i+1, right = nums.length-1;
27-
while(left < right){
25+
for (int i = 0; i < nums.length - 2; i++) {
26+
int left = i + 1, right = nums.length - 1;
27+
while (left < right) {
2828
int sum = nums[i] + nums[left] + nums[right];
29-
if(sum < target) {
30-
result += right-left;//this line is key!
29+
if (sum < target) {
30+
result += right - left;//this line is key!
3131
left++;
3232
} else right--;
3333
}

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,41 @@ public class _26 {
1313

1414
public static int removeDuplicates_editorial_solution(int[] nums) {
1515
int i = 0;
16-
for(int j = 1; j < nums.length; j++){
17-
if(nums[i] != nums[j]){
16+
for (int j = 1; j < nums.length; j++) {
17+
if (nums[i] != nums[j]) {
1818
i++;
1919
nums[i] = nums[j];
2020
}
2121
}
22-
return i+1;
22+
return i + 1;
2323
}
2424

2525

26-
public static void main(String...strings){
27-
int[] nums = new int[]{1,1,2};
26+
public static void main(String... strings) {
27+
int[] nums = new int[]{1, 1, 2};
2828
// int[] nums = new int[]{1,1,2,2,3};
2929
// int[] nums = new int[]{1,1};
3030
System.out.println(removeDuplicates_editorial_solution(nums));
3131
}
3232

33-
/**Same idea as the editorial solution, mine just got more verbose.*/
33+
/**
34+
* Same idea as the editorial solution, mine just got more verbose.
35+
*/
3436
public static int removeDuplicates_my_original(int[] nums) {
3537
int i = 0;
36-
for(int j = i+1; i < nums.length && j < nums.length;){
37-
while(j < nums.length && nums[i] == nums[j]){
38+
for (int j = i + 1; i < nums.length && j < nums.length; ) {
39+
while (j < nums.length && nums[i] == nums[j]) {
3840
j++;
3941
}
40-
if(j == nums.length) j--;
42+
if (j == nums.length) j--;
4143
int temp = nums[j];
42-
nums[j] = nums[i+1];
43-
nums[i+1] = temp;
44-
if(nums[i] != nums[i+1]) i++;
45-
if(j == nums.length) break;
44+
nums[j] = nums[i + 1];
45+
nums[i + 1] = temp;
46+
if (nums[i] != nums[i + 1]) i++;
47+
if (j == nums.length) break;
4648
j++;
4749
}
48-
return i+1;
50+
return i + 1;
4951
}
5052

5153
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public class _260 {
2020
//Approach 1: normal hashmap
2121
public int[] singleNumber(int[] nums) {
2222
Map<Integer, Integer> map = new HashMap();
23-
for(int i : nums){
24-
map.put(i, map.getOrDefault(i, 0)+1);
23+
for (int i : nums) {
24+
map.put(i, map.getOrDefault(i, 0) + 1);
2525
}
2626

2727
int[] res = new int[2];
2828
int index = 0;
29-
for(int key : map.keySet()){
30-
if(map.get(key) == 1) res[index++] = key;
31-
if(index == 2) break;
29+
for (int key : map.keySet()) {
30+
if (map.get(key) == 1) res[index++] = key;
31+
if (index == 2) break;
3232
}
3333
return res;
3434
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@
2424
*/
2525
public class _261 {
2626

27-
public boolean validTree(int n, int[][] edges){
27+
public boolean validTree(int n, int[][] edges) {
2828
int[] nums = new int[n];
29-
for(int i =0; i < n; i++) {
29+
for (int i = 0; i < n; i++) {
3030
nums[i] = i;
3131
}
3232

33-
for(int i = 0; i < edges.length; i++){
33+
for (int i = 0; i < edges.length; i++) {
3434
int x = find(nums, edges[i][0]);
3535
int y = find(nums, edges[i][1]);
3636

37-
if(x == y) {
37+
if (x == y) {
3838
return false;
3939
}
4040

4141
//union
4242
nums[x] = y;
4343
}
4444

45-
return edges.length == n-1;
45+
return edges.length == n - 1;
4646
}
4747

48-
int find(int[] nums, int i){
49-
if(nums[i] == i) {
48+
int find(int[] nums, int i) {
49+
if (nums[i] == i) {
5050
return i;
5151
}
5252
return find(nums, nums[i]);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
public class _263 {
1010

1111
public boolean isUgly(int num) {
12-
if(num == 0) return false;
13-
int[] divisors = new int[]{5,3,2};
14-
for(int divisor : divisors){
15-
while(num%divisor == 0){
12+
if (num == 0) return false;
13+
int[] divisors = new int[]{5, 3, 2};
14+
for (int divisor : divisors) {
15+
while (num % divisor == 0) {
1616
num /= divisor;
1717
}
1818
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ public int nthUglyNumber(int n) {
2020
for (int i = 1; i < n; i++) {
2121
int min = Math.min(Math.min(factor2, factor3), factor5);
2222
ugly[i] = min;
23-
if(factor2 == min) {
23+
if (factor2 == min) {
2424
factor2 = 2 * ugly[++index2];
2525
}
26-
if(factor3 == min) {
26+
if (factor3 == min) {
2727
factor3 = 3 * ugly[++index3];
2828
}
29-
if(factor5 == min) {
29+
if (factor5 == min) {
3030
factor5 = 5 * ugly[++index5];
3131
}
3232
}
33-
return ugly[n-1];
33+
return ugly[n - 1];
3434
}
3535

3636
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
public class _266 {
1717

1818
public boolean canPermutePalindrome(String s) {
19-
19+
2020
char[] chars = s.toCharArray();
2121
Map<Character, Integer> map = new HashMap<Character, Integer>();
22-
for(char c : chars){
23-
if(!map.containsKey(c)) map.put(c, 1);
24-
else map.put(c, map.get(c)+1);
22+
for (char c : chars) {
23+
if (!map.containsKey(c)) map.put(c, 1);
24+
else map.put(c, map.get(c) + 1);
2525
}
2626
int evenCount = 0;
27-
for(Map.Entry<Character, Integer> e : map.entrySet()){
28-
if(e.getValue() % 2 != 0) evenCount++;
29-
if(evenCount > 1) return false;
27+
for (Map.Entry<Character, Integer> e : map.entrySet()) {
28+
if (e.getValue() % 2 != 0) evenCount++;
29+
if (evenCount > 1) return false;
3030
}
3131
return true;
32-
32+
3333
}
3434

3535
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class _268 {
1414
then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up.*/
1515
public int missingNumber(int[] nums) {
1616
int xor = 0, i = 0;
17-
for(; i < nums.length; i++){
18-
xor = xor^i^nums[i];
17+
for (; i < nums.length; i++) {
18+
xor = xor ^ i ^ nums[i];
1919
}
20-
return xor^i;
20+
return xor ^ i;
2121
}
2222

2323
}

0 commit comments

Comments
 (0)