Skip to content

Commit a1ccba5

Browse files
committed
Merge pull request Hearen#15 from zhewen166/master
week one
2 parents 26b9a1e + eb138cc commit a1ccba5

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cz.algorithm.learn.first;
2+
3+
public class RotatedSortedArray {
4+
5+
public static void main(String[] args) {
6+
int[] nums = {5, 6 ,7,0, 1 ,1,2,2,3,3 ,4};
7+
int target = 3;
8+
System.out.println(search(nums, target));
9+
}
10+
public static boolean search(int[] nums, int target) {
11+
int right = nums.length - 1;
12+
int left = 0;
13+
while (left <= right) {
14+
int middle = (left + right)/2 ;
15+
System.out.println(middle + " " + nums[middle]);
16+
if( target == nums[middle]) {
17+
return true;
18+
} else if(target > nums[middle]) {
19+
left = middle + 1;
20+
21+
} else if(target <= nums[middle]) {
22+
right = middle - 1;
23+
}
24+
}
25+
return false;
26+
}
27+
} //done
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.cz.algorithm.learn.first;
2+
3+
public class SumClosest {
4+
5+
public static void main(String[] args) {
6+
int[] nums = {-1, 2 ,1 ,-4, 4};
7+
int target = 1;
8+
System.out.println(threeSumClosest(nums, target));
9+
}
10+
11+
public static int threeSumClosest(int[] nums, int target) {
12+
13+
int len = nums.length;
14+
if(len < 3) {
15+
return 0;
16+
}
17+
int sum = nums[0] + nums[1] + nums[2];
18+
for(int i = 1; i < len - 2 ; i ++) {
19+
int temp = nums[i] + nums[i + 1] + nums[i + 2];
20+
if(Math.abs(sum - target) > Math.abs( temp -target)) {
21+
sum = temp;
22+
}
23+
}
24+
return sum;
25+
}
26+
}//done
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.cz.algorithm.learn.first;
2+
3+
public class TrappingRainWater {
4+
5+
public static void main(String[] args) {
6+
int[] A = {0,1,0,2,1,0,1,3,2,1,2,1};
7+
System.out.println(trap(A));
8+
}
9+
10+
// public static int trap(int[] A) {
11+
// int left = 0;
12+
// int right = A.length - 1; //
13+
// int sum = 0; //the sum of the rain
14+
// int pre = 0; //recode the previous one or more
15+
// while(left < right) {
16+
//
17+
// }
18+
// return 0;
19+
// }
20+
21+
public static int trap(int[] A) {
22+
int left = 0 , right = A.length-1;
23+
int sum = 0;
24+
int pre = 0;
25+
while(left < right){
26+
sum += (Math.min(A[left], A[right])-pre) * (right-left-1);
27+
pre = Math.min(A[left],A[right]);
28+
if(A[left] > A[right]){
29+
int temp = right-1;
30+
while(left < temp && A[temp] <= pre){sum-=A[temp];temp--;}
31+
if(left < temp) sum -= pre;
32+
right = temp;
33+
}else{
34+
int temp = left+1;
35+
while(temp < right && A[temp] <= pre){sum-=A[temp];temp++;}
36+
if(temp < right) sum -= pre;
37+
left = temp;
38+
}
39+
}
40+
return sum;
41+
}
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.cz.algorithm.learn.first;
2+
3+
public class WordSearch {
4+
5+
public static void main(String[] args) {
6+
7+
String word = "ABCCED";
8+
char[][] words = {
9+
{'A','B','C','E'},
10+
{'S','F','C','S'},
11+
{'A','D','E','E'}
12+
};
13+
System.out.println(exist(words,word));
14+
}
15+
/**
16+
* */
17+
public static boolean exist(char[][] board, String word) {
18+
19+
// if(word == "") return true;
20+
// boolean[][] visited = new boolean[board.length][board[0].length];
21+
//
22+
// for(int i = 0 ; i < board.length ; i ++) {
23+
// for(int j = 0; j < board[i].length ; j ++) {
24+
// visited[i][j] = false;
25+
// }
26+
// }
27+
// char [] wordCharArray = word.toCharArray();
28+
// int outLen = board.length;
29+
// int innerLen = board[0].length;
30+
// return false;
31+
32+
int m = board.length;
33+
int n = board[0].length;
34+
boolean[][] visited = new boolean[m][n];
35+
for (int i = 0; i < m; i++) {
36+
for (int j = 0; j < n; j++) {
37+
if (dfs(board, visited, i, j, word, 0)) return true;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
public static boolean dfs(char[][] board, boolean[][] visited, int x, int y, String word, int k) {
44+
if (k == word.length()) return true;
45+
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return false;
46+
if (visited[x][y]) return false;
47+
if (board[x][y] != word.charAt(k)) return false;
48+
49+
visited[x][y] = true;
50+
if (dfs(board, visited, x - 1, y, word, k + 1)) return true;
51+
if (dfs(board, visited, x + 1, y, word, k + 1)) return true;
52+
if (dfs(board, visited, x, y - 1, word, k + 1)) return true;
53+
if (dfs(board, visited, x, y + 1, word, k + 1)) return true;
54+
visited[x][y] = false;
55+
56+
return false;
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.cz.algorithm.learn.first;
2+
3+
public class removeDuplicates {
4+
5+
public static void main(String[] args) {
6+
int[] nums = {1,1,1,2,2,3 };
7+
System.out.println(removeDuplicate(nums));
8+
}
9+
10+
/*
11+
* return the lentgh of the array.
12+
* */
13+
public static int removeDuplicate(int[] A) {
14+
int n = A.length;
15+
if(n==0)return 0;
16+
if(n==1)return 1;
17+
int num=1,i,temp=A[1];
18+
for(i=2;i<n;++i)
19+
if(A[i]!=A[i-2]) {
20+
A[num++]=temp;
21+
temp=A[i];
22+
}
23+
A[num++]=temp;
24+
return num;
25+
}
26+
27+
/**
28+
* compare from the last elements.
29+
* */
30+
// public int removeDuplicates(int[] A) {
31+
// if(A.length ==0){
32+
// return 0;
33+
// }
34+
// int count=1;
35+
// for(int i=1; i<A.length; ++i){
36+
// if(A[i] != A[i-1]){ //注意这行代码
37+
// A[count]=A[i];
38+
// count++;
39+
// }
40+
// }
41+
// return count;
42+
// }
43+
44+
}

0 commit comments

Comments
 (0)