Skip to content

Commit 26cadec

Browse files
committed
代码更新
1 parent c6cd635 commit 26cadec

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

src/BinSearch/Sqrt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package BinSearch;
2-
import java.lang.Math.*;
2+
33
/**
44
* Created by 周杰伦 on 2018/3/16.
55
*/

src/BinSearch/arrangeCoins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Created by 周杰伦 on 2018/3/16.
55
*/
6-
public class arrangeCoins {
6+
public class arrangeCoins {
77
public int arrangeCoins(int n) {
88
if (n <= 1) {
99
return n;

src/BinSearch/singleNonDuplicate.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* Created by 周杰伦 on 2018/3/16.
55
*/
66
public class singleNonDuplicate {
7-
public int singleNonDuplicate(int[] nums) {
7+
public static void main(String[] args) {
8+
int []num = {1,1,2,3,3,4,4,8,8};
9+
System.out.println(singleNonDuplicate(num));
10+
}
11+
public int singleNonDuplicate1(int[] nums) {
812
// binary search
913
int n=nums.length, lo=0, hi=n/2;
1014
while (lo < hi) {
@@ -14,4 +18,30 @@ public int singleNonDuplicate(int[] nums) {
1418
}
1519
return nums[2*lo];
1620
}
21+
public static int singleNonDuplicate(int[] nums) {
22+
int l = 0,r = nums.length - 1;
23+
if (nums.length <= 2) {
24+
return nums[0];
25+
}
26+
if (nums[1] != nums[0]) {
27+
return nums[0];
28+
}
29+
if (nums[nums.length - 1] != nums[nums.length - 2]) {
30+
return nums[nums.length - 1];
31+
}
32+
while (l <= r) {
33+
int m = l + (r - l)/2;
34+
if (nums[m + 1] != nums[m] && nums[m] != nums[m - 1]) {
35+
return nums[m];
36+
}
37+
if (m - 1 >= 0 && nums[m] == nums[m - 1]) {
38+
r = m - 1;
39+
}else if (m + 1 < nums.length && nums[m] == nums[m + 1]) {
40+
l = m + 1;
41+
}else {
42+
return nums[m];
43+
}
44+
}
45+
return nums[l];
46+
}
1747
}

src/DFS/图的连通分量个数.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ public void dfs (int [][]M, int j, int []visit) {
2929
}
3030
}
3131

32+
3233
}
3334

src/DFS/岛屿的最大面积.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package DFS;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/7/18.
5+
*/
6+
public class 岛屿的最大面积 {
7+
public static void main(String[] args) {
8+
int [][]a = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
9+
System.out.println(maxAreaOfIsland(a));
10+
}
11+
public static int maxAreaOfIsland(int[][] grid) {
12+
int [][]visit = new int[grid.length][grid[0].length];
13+
int max = 0;
14+
for (int i = 0;i < grid.length;i ++) {
15+
for (int j = 0;j < grid[0].length;j ++) {
16+
if (grid[i][j] == 1) {
17+
max = Math.max(max, dfs(grid, i, j, visit, 0));
18+
}
19+
}
20+
}
21+
return max;
22+
}
23+
24+
public static int dfs(int [][]grid, int x, int y, int [][]visit, int count) {
25+
if (x < 0 || x > grid.length - 1 || y < 0 || y > grid[0].length - 1) {
26+
return count;
27+
}
28+
if (visit[x][y] == 1 || grid[x][y] == 0) {
29+
return count;
30+
}
31+
32+
visit[x][y] = 1;
33+
count ++;
34+
35+
count += dfs(grid, x + 1, y, visit, 0);
36+
count += dfs(grid, x - 1, y, visit, 0);
37+
count += dfs(grid, x, y + 1, visit, 0);
38+
count += dfs(grid, x, y - 1, visit, 0);
39+
return count;
40+
}
41+
}

src/DFS/查找最大的连通面积.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package DFS;
22

33
import java.util.Arrays;
4+
import java.util.Stack;
45

56
/**
67
* Created by 周杰伦 on 2018/3/30.
@@ -45,4 +46,34 @@ public static void dfs(int [][]grid,int [][]visit, int x, int y) {
4546
dfs(grid,visit,x ,y - 1);
4647
return;
4748
}
49+
50+
//非递归写法
51+
// class Pos{
52+
// int x;
53+
// int y;
54+
// int count;
55+
//
56+
// public Pos(int x, int y, int count) {
57+
// this.x = x;
58+
// this.y = y;
59+
// this.count = count;
60+
// }
61+
// }
62+
// public int stack(int [][]grid,int [][]visit, int x, int y) {
63+
// Stack<Pos> stack = new Stack<>();
64+
// stack.push(new Pos(x, y, 0));
65+
// int [][]pos = {{0,1}, {0, -1}, {1, 0}, {-1, 0}};
66+
// while (!stack.isEmpty()) {
67+
// Pos init = stack.peek();
68+
// for (int i = 0; i < pos.length; i++) {
69+
// int x0 = x + pos[i][0];
70+
// int y0 = y + pos[i][1];
71+
// if (visit[x0][y0] == 0 && grid[x0][y0] == 1) {
72+
// stack.push(new Pos(x0, y0, init.count + 1));
73+
// break;
74+
// }
75+
// }
76+
// stack.pop();
77+
// }
78+
// }
4879
}

0 commit comments

Comments
 (0)