Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 60e9885

Browse files
refactor for format
1 parent 2f50b2f commit 60e9885

39 files changed

+435
-399
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ public class _350 {
2323

2424
public int[] intersect(int[] nums1, int[] nums2) {
2525
Map<Integer, Integer> map1 = new HashMap();
26-
for(int i : nums1){
27-
if(map1.containsKey(i)){
28-
map1.put(i, map1.get(i)+1);
26+
for (int i : nums1) {
27+
if (map1.containsKey(i)) {
28+
map1.put(i, map1.get(i) + 1);
2929
} else {
3030
map1.put(i, 1);
3131
}
3232
}
33-
33+
3434
List<Integer> list = new ArrayList();
35-
for(int i : nums2){
36-
if(map1.containsKey(i) && map1.get(i) > 0){
35+
for (int i : nums2) {
36+
if (map1.containsKey(i) && map1.get(i) > 0) {
3737
list.add(i);
38-
map1.put(i, map1.get(i)-1);
38+
map1.put(i, map1.get(i) - 1);
3939
}
4040
}
41-
41+
4242
int[] result = new int[list.size()];
4343
int i = 0;
44-
for(int num : list){
44+
for (int num : list) {
4545
result[i++] = num;
4646
}
47-
47+
4848
return result;
4949
}
5050

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ public int numberOfPatterns(int m, int n) {
4141
jumps[4][6] = jumps[6][4] = 5;
4242
jumps[7][9] = jumps[9][7] = 8;
4343
jumps[1][7] = jumps[7][1] = 4;
44-
jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1]= 5;
44+
jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5;
4545
jumps[9][3] = jumps[3][9] = 6;
4646
jumps[3][7] = jumps[7][3] = 5;
4747
visited = new boolean[10];
4848
int count = 0;
49-
count += dfs(1, 1, 0, m, n)*4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
50-
count += dfs(2, 1, 0, m, n)*4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
49+
count += dfs(1, 1, 0, m, n) * 4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
50+
count += dfs(2, 1, 0, m, n) * 4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4
5151
count += dfs(5, 1, 0, m, n);
5252
return count;
5353
}
5454

55-
private int dfs(int num, int len, int count, int m, int n){
56-
if(len >= m) count++;
55+
private int dfs(int num, int len, int count, int m, int n) {
56+
if (len >= m) count++;
5757
len++;
58-
if(len > n) return count;
58+
if (len > n) return count;
5959
visited[num] = true;
60-
for(int next = 1; next <= 9; next++){
60+
for (int next = 1; next <= 9; next++) {
6161
int jump = jumps[num][next];
62-
if(!visited[next] && (jump == 0 || visited[jump])){
62+
if (!visited[next] && (jump == 0 || visited[jump])) {
6363
count = dfs(next, len, count, m, n);
6464
}
6565
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,28 @@ public class _352 {
2727

2828
public static class SummaryRanges {
2929

30-
/**Use treemap, key is the start of the interval.*/
30+
/**
31+
* Use treemap, key is the start of the interval.
32+
*/
3133
TreeMap<Integer, Interval> treeMap;
3234

33-
/** Initialize your data structure here. */
35+
/**
36+
* Initialize your data structure here.
37+
*/
3438
public SummaryRanges() {
3539
treeMap = new TreeMap<>();
3640
}
3741

3842
public void addNum(int val) {
39-
if(treeMap.containsKey(val)) return;
43+
if (treeMap.containsKey(val)) return;
4044
Integer lower = treeMap.lowerKey(val);
4145
Integer higher = treeMap.higherKey(val);
42-
if(lower != null && higher != null && treeMap.get(lower).end + 1 == val && higher == val + 1) {
46+
if (lower != null && higher != null && treeMap.get(lower).end + 1 == val && higher == val + 1) {
4347
treeMap.get(lower).end = treeMap.get(higher).end;
4448
treeMap.remove(higher);
45-
} else if(lower != null && treeMap.get(lower).end + 1 >= val) {
49+
} else if (lower != null && treeMap.get(lower).end + 1 >= val) {
4650
treeMap.get(lower).end = Math.max(treeMap.get(lower).end, val);
47-
} else if(higher != null && higher == val + 1) {
51+
} else if (higher != null && higher == val + 1) {
4852
treeMap.put(val, new Interval(val, treeMap.get(higher).end));
4953
treeMap.remove(higher);
5054
} else {

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

+65-59
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@
77

88
/**
99
* Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game.
10-
11-
The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
12-
13-
You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
14-
15-
Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
16-
17-
When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
18-
19-
Example:
20-
Given width = 3, height = 2, and food = [[1,2],[0,1]].
21-
22-
Snake snake = new Snake(width, height, food);
23-
24-
Initially the snake appears at position (0,0) and the food at (1,2).
25-
26-
|S| | |
27-
| | |F|
28-
29-
snake.move("R"); -> Returns 0
30-
31-
| |S| |
32-
| | |F|
33-
34-
snake.move("D"); -> Returns 0
35-
36-
| | | |
37-
| |S|F|
38-
39-
snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )
40-
41-
| |F| |
42-
| |S|S|
43-
44-
snake.move("U"); -> Returns 1
45-
46-
| |F|S|
47-
| | |S|
48-
49-
snake.move("L"); -> Returns 2 (Snake eats the second food)
50-
51-
| |S|S|
52-
| | |S|
53-
54-
snake.move("U"); -> Returns -1 (Game over because snake collides with border)
10+
* <p>
11+
* The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
12+
* <p>
13+
* You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
14+
* <p>
15+
* Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
16+
* <p>
17+
* When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
18+
* <p>
19+
* Example:
20+
* Given width = 3, height = 2, and food = [[1,2],[0,1]].
21+
* <p>
22+
* Snake snake = new Snake(width, height, food);
23+
* <p>
24+
* Initially the snake appears at position (0,0) and the food at (1,2).
25+
* <p>
26+
* |S| | |
27+
* | | |F|
28+
* <p>
29+
* snake.move("R"); -> Returns 0
30+
* <p>
31+
* | |S| |
32+
* | | |F|
33+
* <p>
34+
* snake.move("D"); -> Returns 0
35+
* <p>
36+
* | | | |
37+
* | |S|F|
38+
* <p>
39+
* snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )
40+
* <p>
41+
* | |F| |
42+
* | |S|S|
43+
* <p>
44+
* snake.move("U"); -> Returns 1
45+
* <p>
46+
* | |F|S|
47+
* | | |S|
48+
* <p>
49+
* snake.move("L"); -> Returns 2 (Snake eats the second food)
50+
* <p>
51+
* | |S|S|
52+
* | | |S|
53+
* <p>
54+
* snake.move("U"); -> Returns -1 (Game over because snake collides with border)
5555
*/
5656
public class _353 {
5757
public class SnakeGame {
@@ -63,11 +63,14 @@ public class SnakeGame {
6363
int width;
6464
int height;
6565

66-
/** Initialize your data structure here.
67-
@param width - screen width
68-
@param height - screen height
69-
@param food - A list of food positions
70-
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
66+
/**
67+
* Initialize your data structure here.
68+
*
69+
* @param width - screen width
70+
* @param height - screen height
71+
* @param food - A list of food positions
72+
* E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0].
73+
*/
7174
public SnakeGame(int width, int height, int[][] food) {
7275
this.set = new HashSet();
7376
set.add(0);//initially at [0][0]
@@ -78,17 +81,20 @@ public SnakeGame(int width, int height, int[][] food) {
7881
this.height = height;
7982
}
8083

81-
/** Moves the snake.
82-
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
83-
@return The game's score after the move. Return -1 if game over.
84-
Game over when snake crosses the screen boundary or bites its body. */
84+
/**
85+
* Moves the snake.
86+
*
87+
* @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
88+
* @return The game's score after the move. Return -1 if game over.
89+
* Game over when snake crosses the screen boundary or bites its body.
90+
*/
8591
public int move(String direction) {
86-
if(score == -1) return -1;
92+
if (score == -1) return -1;
8793

8894
//compute head
8995
int rowHead = body.peekFirst() / width;
9096
int colHead = body.peekFirst() % width;
91-
switch(direction){
97+
switch (direction) {
9298
case "U":
9399
rowHead--;
94100
break;
@@ -101,11 +107,11 @@ public int move(String direction) {
101107
default:
102108
colHead++;
103109
}
104-
int newHead = rowHead*width+colHead;
110+
int newHead = rowHead * width + colHead;
105111

106112
set.remove(body.peekLast());//we'll remove the tail from set for now to see if it hits its tail
107113
//if it hits the boundary
108-
if(set.contains(newHead) || rowHead < 0 || colHead < 0 || rowHead >= height || colHead >= width){
114+
if (set.contains(newHead) || rowHead < 0 || colHead < 0 || rowHead >= height || colHead >= width) {
109115
return score = -1;
110116
}
111117

@@ -114,7 +120,7 @@ public int move(String direction) {
114120
body.offerFirst(newHead);
115121

116122
//normal eat case: keep tail, add head
117-
if(foodIndex < food.length && rowHead == food[foodIndex][0] && colHead == food[foodIndex][1]){
123+
if (foodIndex < food.length && rowHead == food[foodIndex][0] && colHead == food[foodIndex][1]) {
118124
set.add(body.peekLast());//old tail does not change, so add it back to set since we removed it earlier
119125
foodIndex++;
120126
return ++score;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public int maxEnvelopes(int[][] envelopes) {
2727
if (a[0] == b[0]) return b[1] - a[1];
2828
else return a[0] - b[0];
2929
}
30-
);
30+
);
3131
int[] dp = new int[envelopes.length];
3232
int len = 0;
3333
for (int[] envelope : envelopes) {
3434
int index = Arrays.binarySearch(dp, 0, len, envelope[1]);
3535
if (index < 0) {
36-
index = -(index+1);
36+
index = -(index + 1);
3737
}
3838
dp[index] = envelope[1];
3939
if (index == len) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public boolean isReflected(int[][] points) {
3333
String str = point[0] + "a" + point[1];
3434
set.add(str);
3535
}
36-
int sum = max+min;
36+
int sum = max + min;
3737
for (int[] p : points) {
3838
String str = (sum - p[0]) + "a" + p[1];
3939
if (!set.contains(str)) return false;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public String rearrangeString(String s, int k) {
4545
while (!heap.isEmpty()) {
4646
Map.Entry<Character, Integer> entry = heap.poll();
4747
stringBuilder.append(entry.getKey());
48-
entry.setValue(entry.getValue()-1);
48+
entry.setValue(entry.getValue() - 1);
4949
waitQueue.offer(entry);
5050
if (waitQueue.size() < k) continue; //there's only k-1 chars in the waitHeap, not full yet
5151
Map.Entry<Character, Integer> front = waitQueue.poll();

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ public class _360 {
2727

2828
//credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution
2929
//in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic
30-
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
31-
int n = nums.length;
32-
int[] sorted = new int[n];
33-
int i = 0, j = n - 1;
34-
int index = a >= 0 ? n - 1 : 0;
35-
while (i <= j) {
36-
if (a >= 0) {
37-
sorted[index--] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[i++], a, b, c) : function(nums[j--], a, b, c);
38-
} else {
39-
sorted[index++] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[j--], a, b, c) : function(nums[i++], a, b, c);
40-
}
30+
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
31+
int n = nums.length;
32+
int[] sorted = new int[n];
33+
int i = 0, j = n - 1;
34+
int index = a >= 0 ? n - 1 : 0;
35+
while (i <= j) {
36+
if (a >= 0) {
37+
sorted[index--] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[i++], a, b, c) : function(nums[j--], a, b, c);
38+
} else {
39+
sorted[index++] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[j--], a, b, c) : function(nums[i++], a, b, c);
4140
}
42-
return sorted;
4341
}
42+
return sorted;
43+
}
4444

4545
public int[] sortTransformedArray_naive(int[] nums, int a, int b, int c) {
4646
int[] result = new int[nums.length];
@@ -52,7 +52,7 @@ public int[] sortTransformedArray_naive(int[] nums, int a, int b, int c) {
5252
}
5353

5454
private int function(int num, int a, int b, int c) {
55-
return a*(num*num) + b*num + c;
55+
return a * (num * num) + b * num + c;
5656
}
5757

5858
}

0 commit comments

Comments
 (0)