Skip to content

Commit 2748ff1

Browse files
committed
双指针
DFS开头
1 parent 12943b2 commit 2748ff1

9 files changed

+316
-0
lines changed

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+
import java.util.Arrays;
4+
5+
/**
6+
* Created by 周杰伦 on 2018/3/30.
7+
*/
8+
public class 图的连通分量个数 {
9+
static int count = 0;
10+
public static int findCircleNum(int[][] M) {
11+
12+
count = 0;
13+
if (M == null)return 0;
14+
int m = M.length;
15+
int n = M[0].length;
16+
int [][]visit = new int[m][n];
17+
for (int i = 0;i < visit.length;i ++) {
18+
Arrays.fill(visit[i],0);
19+
}
20+
21+
for (int i = 0;i < m;i ++) {
22+
for (int j = 0;j < n;j ++) {
23+
if (M[i][j] == 1 && visit[i][j] != 1) {
24+
dfs(M, visit, i, j);
25+
count ++;
26+
}
27+
}
28+
}
29+
return count;
30+
}
31+
public static void dfs(int [][]grid,int [][]visit, int x, int y) {
32+
if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length ||grid[x][y] == 0 || visit[x][y] == 1) return;
33+
34+
visit[x][y] = 1;
35+
dfs(grid,visit,x + 1,y);
36+
dfs(grid,visit,x,y + 1);
37+
dfs(grid,visit,x - 1,y);
38+
dfs(grid,visit,x ,y - 1);
39+
return;
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package DFS;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by 周杰伦 on 2018/3/30.
7+
*/
8+
public class 查找最大的连通面积 {
9+
public static void main(String[] args) {
10+
int [][]a = {{0,1},{1,1}};
11+
System.out.println(maxAreaOfIsland(a));
12+
}
13+
static int max = 0;
14+
static int count = 0;
15+
public static int maxAreaOfIsland(int[][] grid) {
16+
max = 0;
17+
count = 0;
18+
if (grid == null)return 0;
19+
int m = grid.length;
20+
int n = grid[0].length;
21+
int [][]visit = new int[m][n];
22+
for (int i = 0;i < visit.length;i ++) {
23+
Arrays.fill(visit[i],0);
24+
}
25+
26+
for (int i = 0;i < m;i ++) {
27+
for (int j = 0;j < n;j ++) {
28+
count = 0;
29+
if (grid[i][j] == 1) {
30+
dfs(grid, visit, i, j);
31+
}
32+
if (count > max) max = count;
33+
}
34+
}
35+
return max;
36+
}
37+
public static void dfs(int [][]grid,int [][]visit, int x, int y) {
38+
if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length ||grid[x][y] == 0 || visit[x][y] == 1) return;
39+
40+
visit[x][y] = 1;
41+
count ++;
42+
dfs(grid,visit,x + 1,y);
43+
dfs(grid,visit,x,y + 1);
44+
dfs(grid,visit,x - 1,y);
45+
dfs(grid,visit,x ,y - 1);
46+
return;
47+
}
48+
}

src/双指针/两数平方和.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package 双指针;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/3/30.
5+
*/
6+
public class 两数平方和 {
7+
public boolean judgeSquareSum(int c) {
8+
double i = 0,j = Math.floor(Math.pow(c,0.5));
9+
while (i <= j) {
10+
if (i <= j && Math.pow(i,2) + Math.pow(j,2) == c) return true;
11+
else if (i <= j && Math.pow(i,2) + Math.pow(j,2) > c) {
12+
j --;
13+
}
14+
else if (i <= j && Math.pow(i,2) + Math.pow(j,2) < c) {
15+
i ++;
16+
}
17+
}
18+
return false;
19+
}
20+
}

src/双指针/单链表判环.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package 双指针;
2+
/**
3+
* Definition for singly-linked list.
4+
5+
* }
6+
*/
7+
/**
8+
* Created by 周杰伦 on 2018/3/30.
9+
*/
10+
11+
class ListNode {
12+
int val;
13+
ListNode next;
14+
15+
ListNode(int x) {
16+
val = x;
17+
next = null;
18+
}
19+
}
20+
public class 单链表判环 {
21+
22+
public class Solution {
23+
public boolean hasCycle(ListNode head) {
24+
if (head == null)return false;
25+
if (head.next == head) return true;
26+
ListNode slow = head;
27+
ListNode fast = head;
28+
while (slow.next != null && fast.next != null && fast.next.next != null) {
29+
slow = slow.next;
30+
fast = fast.next.next;
31+
if (slow == fast)return true;
32+
}
33+
return false;
34+
}
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package 双指针;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/3/30.
5+
*/
6+
public class 反转字符串中的元音字符 {
7+
public static void main(String[] args) {
8+
String s = "hello";
9+
System.out.println(reverseVowels(s));
10+
}
11+
public static String reverseVowels(String str) {
12+
int i = 0,j = str.length() - 1;
13+
char[] arr = str.toCharArray();
14+
while (i < j) {
15+
while (i < j && !vowels(arr[i])) {
16+
i ++;
17+
}
18+
while (i < j && !vowels(arr[j])) {
19+
j --;
20+
}
21+
char temp = arr[i];
22+
arr[i] = arr[j];
23+
arr[j] = temp;
24+
i ++;
25+
j --;
26+
}
27+
return String.valueOf(arr);
28+
}
29+
public static boolean vowels(char c) {
30+
if (c == 'i' || c =='o' || c == 'a' || c =='e' || c == 'u')return true;
31+
else if (c == 'I' || c == 'O' || c == 'A' || c == 'E' || c == 'U') return true;
32+
else return false;
33+
}
34+
}

src/双指针/回文字符串.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package 双指针;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/3/30.
5+
*/
6+
public class 回文字符串 {
7+
public static void main(String[] args) {
8+
System.out.println(validPalindrome("aba"));
9+
}
10+
public static boolean validPalindrome(String s) {
11+
int i = 0,j = s.length() - 1;
12+
while (i < j) {
13+
if (s.charAt(i) != s.charAt(j)) {
14+
return Palindrome(s,i + 1, j) || Palindrome(s, i, j - 1);
15+
}
16+
i ++;
17+
j --;
18+
}
19+
return true;
20+
}
21+
public static boolean Palindrome(String s, int l, int r) {
22+
int i = l,j = r;
23+
while (i < j) {
24+
if (s.charAt(i) != s.charAt(j)) {
25+
return false;
26+
}
27+
i ++;
28+
j --;
29+
}
30+
return true;
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package 双指针;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/3/30.
5+
*/
6+
public class 归并两个有序数组 {
7+
public void merge(int[] nums1, int m, int[] nums2, int n) {
8+
if (nums1.length == 1 && nums2.length == 0) return;
9+
if (nums1.length == 1 && nums2 != null)nums1[0] = nums2[0];
10+
int i = m - 1,j = n - 1;
11+
int index = nums1.length - 1;
12+
while (i >= 0 && j >= 0) {
13+
if (nums1[i] > nums2[j]) {
14+
nums1[index --] = nums1[i];
15+
nums1[i] = 0;
16+
i --;
17+
}
18+
else {
19+
nums1[index --] = nums2[j];
20+
j --;
21+
}
22+
}
23+
}
24+
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package 双指针;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.*;
5+
6+
/**
7+
* Created by 周杰伦 on 2018/3/30.
8+
*/
9+
public class 最长子序列加强版 {
10+
public static void main(String[] args) {
11+
List<String> d = new ArrayList<>();
12+
d.add("a");
13+
d.add("b");
14+
15+
Collections.sort(d, new Comparator<String>() {
16+
@Override
17+
public int compare(String o1, String o2) {
18+
if (o2.length() == o1.length()) return o1.compareTo(o2);
19+
return o2.length() - o1.length();
20+
}
21+
});
22+
System.out.println(Arrays.toString(d.toArray()));
23+
}
24+
public String findLongestWord(String s, List<String> d) {
25+
if (s == null || s.equals(""))return "";
26+
if (d == null || d.size() == 0)return "";
27+
Collections.sort(d, new Comparator<String>() {
28+
@Override
29+
public int compare(String o1, String o2) {
30+
if (o2.length() == o1.length()) return o1.compareTo(o2);
31+
return o2.length() - o1.length();
32+
}
33+
});
34+
for (String sub : d) {
35+
int i = 0;
36+
int j = 0;
37+
while (i < s.length() && j < sub.length()) {
38+
if (s.charAt(i) != sub.charAt(j)) {
39+
i ++;
40+
}
41+
else {
42+
i ++;
43+
j ++;
44+
}
45+
if (j == sub.length()) {
46+
return sub;
47+
}
48+
}
49+
}
50+
return "";
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package 双指针;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/3/29.
5+
*/
6+
public class 查找和为9的数对 {
7+
public static void main(String[] args) {
8+
9+
}
10+
public int[] twoSum(int[] numbers, int target) {
11+
int i = 0,j = numbers.length - 1;
12+
int []index = new int[2];
13+
while (i < j) {
14+
if (numbers[i] + numbers[j] < target) {
15+
i ++;
16+
}
17+
else if (numbers[i] + numbers[j] > target) {
18+
j --;
19+
}
20+
else {
21+
index[0] = i + 1;
22+
index[1] = j + 1;
23+
break;
24+
}
25+
}
26+
return index;
27+
}
28+
}

0 commit comments

Comments
 (0)