12周刷题大作战

又忘记带笔和本来图书馆了,只能继续复习。
复习: 163,38 (中午不到一小时) 228, 26, 27, 80(晚饭后)

LeetCode - 复习

163. Missing Ranges

又忘记int最大值的问题了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class  {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> list = new ArrayList<>();

if(nums.length == 0){
list.add(group(lower, upper));
return list;
}

if(nums[0] > lower){
list.add(group(lower, nums[0]-1));
}

for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[i-1]+1 && nums[i] != nums[i-1]){
list.add(group(nums[i-1]+1, nums[i]-1));
}
}

if(nums[nums.length - 1] < upper){
list.add(group(nums[nums.length - 1]+1, upper));
}


return list;
}
private String group(int start, int end){
if(start == end){
return start + "";
}else{
return start + "->" + end;
}
}
}

38. Count and Say

StringBuilder sb 添加 1B 要先append(1)再append(B)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class  {
public String countAndSay(int n) {
String result = "1";

for(int i=2; i <= n; i++){
StringBuilder sb = new StringBuilder();
char pre = result.charAt(0);
int num = 1;
for(int j = 1; j<result.length(); j++){
char cur = result.charAt(j);
if(cur != pre){
sb.append(num);
sb.append(pre);
pre = result.charAt(j);
num = 1;
}else{
num++;
}
}
sb.append(num);
sb.append(pre);
result = sb.toString();
}
return result;
}
}

228. Summary Ranges

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class  {
public List<String> summaryRanges(int[] nums) {
if(nums.length == 0){
return new ArrayList<>();
}

List<String> list = new ArrayList<>();
int start = 0;

for(int i=1; i < nums.length; i++){
if(nums[i] != nums[i-1] +1 && nums[i] != nums[i-1]){
list.add(group(nums[start], nums[i-1]));
start = i;
}
}

if(start < nums.length){
list.add(group(nums[start], nums[nums.length-1]));
}

return list;


}

private String group(int start, int end){
if(start == end){
return start + "";
}else{
return start + "->" + end;
}
}
}

26. Remove Duplicates from Sorted Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class  {
public int removeDuplicates(int[] nums) {
if(nums.length <= 1){
return nums.length;
}

int start = 0;
for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[i-1]){
start++;
nums[start] = nums[i];
}
}

return start+1;
}
}

27. Remove Element

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class  {
public int removeElement(int[] nums, int val) {
if(nums.length == 0){
return 0;
}

int start = -1;

for(int i=0; i < nums.length; i++){
if(nums[i] != val){
start++;
nums[start] = nums[i];
}
}
return start+1;
}
}

80. Remove Duplicates from Sorted Array 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length < 2){
return nums.length;
}

int start = 1;
for(int i=2; i < nums.length; i++){
if(nums[i] != nums[start-1]){
start++;
nums[start] = nums[i];
}
}

return start+1;
}
}

LeetCode - 735 (0 Easy, 1 Medium, 0 Hard)

735. Asteroid Collision - Medium


一段报错的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int[] asteroidCollision(int[] asteroids) {
if(asteroids.length == 0){
return new int[0];
}

Stack<Integer> stack= new Stack<>();
stack.push(asteroids[0]);

for(int i = 1; i < asteroids.length; i++){
if(asteroids[i] > 0){
stack.push(asteroids[i]);
}else{
while(asteroids[i] + stack.peek() < 0){
stack.pop();
}
if(asteroids[i] + stack.peek() == 0){
stack.pop();
}

}
}

int[] result = new int[stack.size()];
int i = 0;
for(int val : stack){
result[i++] = val;
}

return result;
}
}

正确的代码,太多种情况了,一定要想清楚

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public int[] asteroidCollision(int[] a) {
if(a.length == 0){
return new int[0];
}
Stack<Integer> stack= new Stack<>();
stack.push(a[0]);

for(int i = 1; i < a.length; i++){
if(stack.isEmpty() || stack.peek()<0){
stack.push(a[i]);
}else{
if(a[i] > 0){
stack.push(a[i]);
}else{
while(!stack.isEmpty() && stack.peek() > 0 && a[i] + stack.peek() < 0){
stack.pop();
}
if(!stack.isEmpty() && a[i] + stack.peek() == 0){
stack.pop();
}else if(stack.isEmpty() || a[i] + stack.peek() < 0){
stack.push(a[i]);
}
}
}

}

int[] result = new int[stack.size()];
int i = 0;
for(int val:stack){
result[i] = val;
i++;
}
return result;
}
}