Skip to content

Commit 7ce1e3f

Browse files
committed
回溯法 开头 基础+排列
1 parent 9d45da6 commit 7ce1e3f

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

src/BackTracking/IP地址划分.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package BackTracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by 周杰伦 on 2018/4/1.
8+
*/
9+
//1111
10+
//222 222 222 222
11+
// 55 55 55 55
12+
public class IP地址划分 {
13+
public static void main(String[] args) {
14+
15+
}
16+
public List<String> restoreIpAddresses(String s) {
17+
int i,j,k,m;
18+
ArrayList <String> list = new ArrayList<>();
19+
int len = s.length();
20+
for (i = 1;i< 4 && i < len - 2;i ++) {
21+
for (j = i + 1;j < i + 4 && j < len - 1;j ++) {
22+
for (m = j + 1;m < j + 4 && m < len;m ++) {
23+
//substring后面的下标是不算在内的。
24+
String s1 = s.substring(0,i);
25+
String s2 = s.substring(i,j);
26+
String s3 = s.substring(j,m);
27+
String s4 = s.substring(m,len);
28+
if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4))
29+
{
30+
list.add(s1 + '.' + s2 + '.' + s3 + '.' + s4);
31+
}
32+
}
33+
}
34+
}
35+
return list;
36+
}
37+
38+
public boolean isValid(String s) {
39+
if (s.length() == 0 || s.length() > 3 || s.charAt(0) == '0' && s.length() > 1
40+
|| Integer.parseInt(s) > 255) {
41+
return false;
42+
}else return true;
43+
}
44+
45+
// public List<String> restoreIpAddresses(String s) {
46+
// List<String> result = new ArrayList<>();
47+
// doRestore(result, "", s, 0);
48+
// return result;
49+
// }
50+
51+
// private void doRestore(List<String> result, String path, String s, int k) {
52+
// if (s.isEmpty() || k == 4) {
53+
// if (s.isEmpty() && k == 4)
54+
// result.add(path.substring(1));
55+
// return;
56+
// }
57+
// for (int i = 1; i <= (s.charAt(0) == '0' ? 1 : 3) && i <= s.length(); i++) { // Avoid leading 0
58+
// String part = s.substring(0, i);
59+
// if (Integer.valueOf(part) <= 255)
60+
// doRestore(result, path + "." + part, s.substring(i), k + 1);
61+
// }
62+
// }
63+
64+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package BackTracking;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
/**
9+
* Created by 周杰伦 on 2018/4/1.
10+
*/
11+
public class 找出所有数字组合 {
12+
public static void main(String[] args) {
13+
int []a = {1,2,3};
14+
System.out.println(Arrays.toString(permute(a).toArray()));
15+
}
16+
public static List<List<Integer>> permute(int[] nums) {
17+
int m = nums.length;
18+
int []visit = new int[m];
19+
List<List<Integer>> list = new ArrayList<>();
20+
ArrayList<Integer> arr = new ArrayList<>();
21+
dfs(arr, nums, visit, list);
22+
return list;
23+
}
24+
public static void dfs(ArrayList<Integer> arr, int []nums, int[]visit, List<List<Integer>> list) {
25+
if (arr.size() == nums.length) {
26+
list.add(new ArrayList<>(arr));
27+
return;
28+
}
29+
for (int i = 0;i < nums.length;i ++) {
30+
if (visit[i] == 1) continue;
31+
visit[i] = 1;
32+
arr.add(nums[i]);
33+
dfs(arr, nums, visit,list);
34+
arr.remove(arr.size()-1);
35+
visit[i] = 0;
36+
}
37+
38+
return;
39+
}
40+
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package BackTracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by 周杰伦 on 2018/4/1.
8+
*/
9+
public class 数字键打出字母组合 {
10+
public List<String> letterCombinations(String digits) {
11+
if (digits == null || digits.equals(""))return new ArrayList<>();
12+
List<String> list = new ArrayList<>();
13+
combineStr("",0,digits,list);
14+
return list;
15+
}
16+
public void combineStr(String prefix, int offset, String digits, List<String>ret) {
17+
if (offset == digits.length()) {
18+
ret.add(prefix);
19+
return;
20+
}
21+
String letters = KEYS[digits.charAt(offset) - '0'];
22+
char[] arr = letters.toCharArray();
23+
for (char c : arr) {
24+
combineStr(prefix + c,offset + 1,digits, ret);
25+
}
26+
}
27+
private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
28+
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package BackTracking;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/1.
5+
*/
6+
public class 矩阵查找单词 {
7+
public static void main(String[] args) {
8+
char [][]a = {{'a','b'}};
9+
String b = "ba";
10+
System.out.println(b.charAt(0));
11+
System.out.println(b.charAt(0) == a[0][1]);
12+
System.out.println(exist(a,b));
13+
}
14+
public static boolean exist(char[][] board, String word) {
15+
boolean flag = false;
16+
int m = board.length;
17+
int n = board[0].length;
18+
int [][]visit = new int[m][n];
19+
for (int i = 0;i < m;i ++) {
20+
for (int j = 0; j < n; j++) {
21+
if (board[i][j] == word.charAt(0)) {
22+
if (find(board, word, i, j, visit, 0)) {
23+
return true;
24+
}
25+
}
26+
}
27+
}
28+
return flag;
29+
30+
}
31+
public static boolean find(char[][]board, String word, int i,int j,int[][]visit, int offset)
32+
{
33+
if (offset == word.length()) {
34+
return true;
35+
}
36+
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length ||
37+
word.charAt(offset) != board[i][j] || visit[i][j] == 1) {
38+
return false;
39+
}
40+
41+
visit[i][j] = 1;
42+
boolean flag = find(board, word, i + 1, j, visit, offset + 1)
43+
||find(board, word, i, j + 1, visit, offset + 1)
44+
||find(board, word, i - 1, j, visit, offset + 1)
45+
||find(board, word, i, j - 1, visit, offset + 1);
46+
visit[i][j] = 0;
47+
return flag;
48+
49+
}
50+
51+
52+
}

0 commit comments

Comments
 (0)