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的更多相关文章

  1. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  2. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  3. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  4. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  5. 93. Restore IP Addresses产生所有可能的ip地址

    [抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...

  6. leetcode 93 Restore IP Addresses ----- java

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  7. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. LeetCode OJ 93. Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  9. 93. Restore IP Addresses(dfs)

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

随机推荐

  1. swtich多个case使用同一操作

    switch (expression) { case 'status01': case 'status02': case 'status03': alert('resultOne'); break; ...

  2. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  3. Neo4j--节点的增删查改基本用法

    注 node-name 和  label-name node-name 有点句柄的味道. 从面向对象来理解,label-name相当于一个类,node-name相当于这个类的对象. 类比关系型数据库的 ...

  4. 洛谷 P5661 公交换乘(队列)

    题目传送门 解题思路: 暴力模拟. AC代码: #include<iostream> #include<cstdio> #include<queue> using ...

  5. winform使用钩子限制windows热键

    新增类KeybordHookProc using System; using System.Collections.Generic; using System.Diagnostics; using S ...

  6. 十五、Numpy-科学计算基础库

    Numpy:          NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等.其提供最核心类型为多维数组类型(ndarray) ...

  7. 2019.3.12 linux关于用户的一些命令

    su:默认切换到root 创建用户 adduser :新建一个用户 sudo adduser 新用户名字 :创建新用户 sudo passwd 用户名:修改该用户名的密码 创建组 sudo addgr ...

  8. CF round #622 (div2)

    CF Round 622 div2 A.简单模拟 B.数学 题意: 某人A参加一个比赛,共n人参加,有两轮,给定这两轮的名次x,y,总排名记为两轮排名和x+y,此值越小名次越前,并且对于与A同分者而言 ...

  9. 25.docker compose 简介 和 docker-compose.yml 参数介绍

    1. docker compose概念 文档  https://docs.docker.com/compose/compose-file/compose-versioning 一个基于 docker ...

  10. 什么?你还没女朋友?教你如何借助Python俘获女孩子芳心!

    天气降温,感情却升温了? 上午刚到公司,就收到小Q发来的灵魂拷问: “Q仔!要不然下午请个假!我带你去精神科看看!?”我实在忍不了,脱口而出. 话音未落,前排的运营小花回头看向小Q,莞尔一笑,百媚横生 ...