Skip to content

Commit 7df77b0

Browse files
committed
week one
1 parent 57bd506 commit 7df77b0

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.zyy.week1;
2+
3+
public class RemoveDuplicates {
4+
5+
//大神级别
6+
public int removeDuplicatesUp(int[] nums) {
7+
int i = 0;
8+
for (int target : nums) {
9+
if (i < 2 || nums[i - 2] < target) {
10+
nums[i++] = target;
11+
}
12+
}
13+
for (int k = 0; k < i; k++) {
14+
System.out.println(nums[k]);
15+
}
16+
return i;
17+
}
18+
19+
//菜鸟级别
20+
public int removeDuplicates(int[] nums) {
21+
int record = 0;
22+
int len = nums.length;
23+
int[] target = new int[len];
24+
int flag = nums[0];
25+
target[record++] = flag;
26+
int duplicate = 1;
27+
for (int i = 1; i < nums.length; i++) {
28+
if (nums[i] == flag) {
29+
duplicate++;
30+
if (duplicate > 2) {
31+
len--;
32+
} else {
33+
target[record++] = flag;
34+
}
35+
} else {
36+
flag = nums[i];
37+
if (duplicate != 1) {
38+
duplicate = 1;
39+
}
40+
target[record++] = flag;
41+
}
42+
}
43+
for (int i = 0; i < len; i++) {
44+
nums[i] = target[i];
45+
}
46+
return len;
47+
}
48+
49+
public static void main(String[] args) {
50+
int[] nums = { 1, 1, 1, 2, 2, 3, 3, 3 };
51+
RemoveDuplicates rd = new RemoveDuplicates();
52+
System.out.println(rd.removeDuplicatesUp(nums));
53+
}
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.zyy.week1;
2+
3+
public class SearchInRotatedSortedArray {
4+
public int search1(int[] nums, int target) {
5+
int len = nums.length;
6+
for (int k = 0; k < len; k++) {
7+
if (target == nums[k])
8+
return k;
9+
else {
10+
if (k != len - 1) {
11+
if (target > nums[k] && nums[k] > nums[k + 1]) {
12+
return -1;
13+
}
14+
}
15+
}
16+
}
17+
return -1;
18+
}
19+
20+
public static void main(String[] args) {
21+
SearchInRotatedSortedArray sisa = new SearchInRotatedSortedArray();
22+
int[] nums = { 3, 4, 5, 6, 0, 1 };
23+
System.out.println(sisa.search1(nums, 1));
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.zyy.week1;
2+
3+
public class SearchInRotatedSortedArrayII {
4+
5+
public boolean search1(int[] nums, int target) {
6+
int len = nums.length;
7+
for (int k = 0; k < len; k++) {
8+
if (target == nums[k])
9+
return true;
10+
else {
11+
if (k != len - 1) {
12+
if (target > nums[k] && nums[k] > nums[k + 1]) {
13+
return false;
14+
}
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
public static void main(String[] args) {
22+
SearchInRotatedSortedArrayII si = new SearchInRotatedSortedArrayII();
23+
int[] nums = { 3, 3, 4, 4, 5, 6, 0, 1 };
24+
System.out.println(si.search1(nums, 5));
25+
}
26+
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.zyy.week1;
2+
3+
import java.util.Arrays;
4+
5+
public class ThreeSumClosest {
6+
7+
public int binarySearchClosest(int[] num, int target, int start, int end){
8+
if(end == start + 1){
9+
if(target - num[start] >= num[end] - target) return end;
10+
else return start;
11+
}
12+
else if(end <= start) return start;
13+
int mid = (start + end)/2;
14+
if(num[mid] == target) return mid;
15+
else if(num[mid] < target) return binarySearchClosest(num, target, mid, end);
16+
else return binarySearchClosest(num, target, start, mid);
17+
}
18+
19+
public int threeSumClosest(int[] num, int target) {
20+
Arrays.sort(num);
21+
int start = 0, end = num.length - 1;
22+
int sumValue = 0;
23+
int offset = Integer.MAX_VALUE;
24+
while(start < end - 1){
25+
int item = target - num[start]- num[end];
26+
int idx = binarySearchClosest(num, item, start + 1, end - 1);
27+
if(idx == start) idx = start+1;
28+
else if(idx == end) idx = end -1;
29+
if(num[start] + num[end] + num[idx] != target){
30+
int oldOfset = offset;
31+
offset = Math.min(offset, Math.abs(target - num[start] - num[end] - num[idx]));
32+
if(offset != oldOfset){
33+
sumValue = num[start] + num[idx] + num[end];
34+
}
35+
}
36+
if(num[start] + num[end] + num[idx] > target) end--;
37+
else if(num[start] + num[end] + num[idx] < target) start++;
38+
else {sumValue = target;break;}
39+
}
40+
return sumValue;
41+
}
42+
43+
public static void main(String[] args) {
44+
int[] nums = { -1, 2, 1, -4 };
45+
ThreeSumClosest tsc = new ThreeSumClosest();
46+
System.out.println(tsc.threeSumClosest(nums, 1));
47+
}
48+
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.zyy.week1;
2+
3+
public class TrappingRainWater {
4+
// 左右两边同时检测,如果右边比左边高,则左边比当前高度低的可以加上差值;
5+
// 如果左边比右边高,则右边比当前高度低的可以加上差值。
6+
public int trap(int[] height) {
7+
int trap = 0;
8+
int level = 0;
9+
int left = 0, right = height.length - 1;
10+
while (left < right) {
11+
if (height[left] < height[right]) {
12+
level = Math.max(height[left], level);
13+
trap += level - height[left];
14+
left++;
15+
} else {
16+
level = Math.max(height[right], level);
17+
trap += level - height[right];
18+
right--;
19+
}
20+
}
21+
return trap;
22+
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] height = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
27+
TrappingRainWater trw = new TrappingRainWater();
28+
System.out.println(trw.trap(height));
29+
}
30+
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.zyy.week1;
2+
3+
public class WordSearch {
4+
5+
public boolean isExist(char[][] board, char[] words, int y, int x, int i) {
6+
if (i == words.length)
7+
return true;
8+
if (y < 0 || x < 0 || y == board.length || x == board[y].length)
9+
return false;
10+
if ((board[y][x] != words[i]))
11+
return false;
12+
board[y][x] ^= 256;
13+
boolean exist = isExist(board, words, y, x + 1, i + 1)
14+
|| isExist(board, words, y + 1, x, i + 1)
15+
|| isExist(board, words, y, x - 1, i + 1)
16+
|| isExist(board, words, y - 1, x, i + 1);
17+
board[y][x] ^= 256;
18+
return exist;
19+
20+
}
21+
22+
// 回溯
23+
public boolean exist(char[][] board, String word) {
24+
// 存放word单个字母
25+
char[] words = word.toCharArray();
26+
for (int i = 0; i < board.length; i++) {
27+
for (int j = 0; j < board[i].length; j++) {
28+
if (isExist(board, words, i, j, 0))
29+
return true;
30+
}
31+
}
32+
return false;
33+
}
34+
35+
public static void main(String[] args) {
36+
char[][] board = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' },
37+
{ 'A', 'D', 'E', 'E' } };
38+
WordSearch ws = new WordSearch();
39+
System.out.println(ws.exist(board, "ABGGGG"));
40+
41+
}
42+
}

0 commit comments

Comments
 (0)