-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM74.java
More file actions
48 lines (46 loc) · 1.47 KB
/
BM74.java
File metadata and controls
48 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package NiukeTOP101;
import java.util.ArrayList;
/**
* 递归 + 回溯
*/
public class BM74 {
//记录分段IP数字字符串
private String nums = "";
//step表示第几个数字,index表示字符串下标
public void dfs(String s, ArrayList<String> res, int step, int index){
//当前分割出的字符串
String cur = "";
//分割出了四个数字
if(step == 4){
//下标必须走到末尾
if(index != s.length())
return;
res.add(nums);
}else{
//最长遍历3位
for(int i = index; i < index + 3 && i < s.length(); i++){
cur += s.charAt(i);
//转数字比较
int num = Integer.parseInt(cur);
String temp = nums;
//不能超过255且不能有前导0
if(num <= 255 && (cur.length() == 1 || cur.charAt(0) != '0')){
//添加点
if(step - 3 != 0)
nums += cur + ".";
else
nums += cur;
//递归查找下一个数字
dfs(s, res, step + 1, i + 1);
//回溯
nums = temp;
}
}
}
}
public ArrayList<String> restoreIpAddresses (String s) {
ArrayList<String> res = new ArrayList<String>();
dfs(s, res, 0, 0);
return res;
}
}