[LC] 93. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output:["255.255.11.135", "255.255.111.35"]
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<>();
helper(res, 0, 0, s, "");
return res;
}
private void helper(List<String> res, int count, int index, String s, String str) {
if (count > 4) {
return;
}
if (count == 4 && index == s.length()) {
res.add(str);
}
for (int i = 1; i < 4; i++) {
if (index + i > s.length()) {
break;
}
String cur = s.substring(index, index + i);
if (cur.charAt(0) == '0' && cur.length() > 1 || cur.length() == 3 && Integer.parseInt(cur) > 255) {
continue;
}
String signal = count == 3 ? "": ".";
helper(res, count + 1, index + i, s, str + cur + signal);
}
}
}
[LC] 93. Restore IP Addresses的更多相关文章
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 93. Restore IP Addresses产生所有可能的ip地址
[抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 【一天一道LeetCode】#93. Restore IP Addresses
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 93. Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- 93. Restore IP Addresses(dfs)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- MySQL数据类型使用总结,浮点使用注意事项
1.对于精度要求较高的应用中,建议使用定点数来存储数值,以保证结果的准确性. 2.对于字符类型,要根据存储引擎进行相应的选择 3.对含有TEXT和BOLB字段的表,如果经常做删除和修改记录的操作要定时 ...
- Linux-课后练习(第二章命令)20200217-1
- 小白需要了解的Ajax和websocket的区别以及使用场景!
在我们日常使用的互联网产品中,很多都是前后端数据的交互来完成的,说到数据交互就不得不提Ajax和websocket,它们可是数据交互的利器,那么它们分别是什么?websocket与Ajax轮询的区别又 ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
- 【Gson】网页上String获取的Json数据转化为对象
1.网络上获取的String Json格式转化为对象获取数据: 需要的包:Gson Maven依赖: <!-- https://mvnrepository.com/artifact/com.go ...
- Eureka高可用环境搭建
1.创建govern-center 子工程 包结构:com.dehigher.govern.center 2.pom文件 (1)父工程pom,用于依赖版本管理 <dependencyManage ...
- awk grep sed 的一些问题
条件 匹配 打印含关键字的行 ps aux | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' z ...
- symmetry methods for differential equations,exercise 1.4
tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...
- Println(Object)小贴士
println public void println(Object x) 打印 Object,然后终止该行.此方法首先调用 String.valueOf(x) 获取打印对象的字符串值,然后的行为如同 ...
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...