Given a string containing only digits, restore it by returning all possible valid IP address combinations.
A valid IP address consists of exactly four integers (each integer is between 0 and 255) separated by single points.
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 '.' 分隔。
**示例:**
~~~
输入: “25525511135”
输出: ["255.255.11.135", "255.255.111.35"]
~~~
```
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function(s) {
let res = [];
if(s.length<4 || s.length>12) return res;//判断边界
dfs(res,s,"",0);
return res;
function dfs(res,s,sub,index){
if(index == 4 && s.length == 0) res.push(sub.substring(1));
if(index == 4 || s.length == 0) return;
dfs(res,s.substring(1),sub+"."+s.substring(0,1),index+1);//一位数
if(s.charAt(0) !='0' && s.length > 1){//两位数
dfs(res,s.substring(2),sub + "." + s.substring(0,2),index+1);
if(s.length > 2 && parseInt(s.substring(0,3)) <= 255){//三位数
dfs(res,s.substring(3),sub+"."+s.substring(0,3),index+1);
}
}
}
};
```
```
var restoreIpAddresses = function(str) {
// 判断最大边界
if(str.length > 12) return []
// 保存所有符合条件的IP地址
let r = []
// 分四步递归处理ip分段
let search = (cur, sub) => {
// 边界条件
if (cur.length === 4 && cur.join('') === str) {
// 过滤 001 010等情况
if(cur[3].length > 1 && cur[3][0] == 0){
return false
}
r.push(cur.join('.'))
} else {
// 正常的处理过程
for (let i = 0, len = Math.min(3, sub.length), tmp; i < len; i++) {
tmp = sub.substr(0, i + 1)
// 过滤 001 010等情况
if (tmp.length > 1 && tmp[0] == 0) {
return false
}
if (tmp < 256) {
search(cur.concat([tmp]), sub.substr(i + 1))
}
}
}
}
search([], str)
return r
}
```
- 说明
- 排序
- java实现
- 二分归并排序
- 快速排序
- js实现
- 冒泡排序 (Bubble Sort)
- 快速排序-js
- ES6-rest参数
- 比较排序
- 堆排序
- 插入排序
- 归并排序
- 选择排序
- shellSort
- 排序基础知识
- 其他
- Rabbit
- chickenProblem
- 汉诺塔
- 取出两个字符串中开头相同的部分
- 两个七进制输入,输出七进制
- 二维数组(矩阵)对角线输出
- 根据概率取值
- 最小编辑距离算法
- 列划分算法
- 数组去重
- 数组去重1
- 在一天里,钟表的时针和分针会重合多少次?
- 找第二大问题
- Raft算法
- B-tree
- Leetcode JAVA实现
- leetcode1. 2 Sum
- leetcode2. Add Two Numbers
- leetcode7. Reverse Integer
- leetcode8. String to Integer (atoi)
- leetcode12. Integer to Roman
- leetcode13. Roman to Integer
- leetcode15. 3Sum
- leetcode18. 4Sum
- leetcode20. Valid Parentheses
- leetcode26. Remove Duplicates from Sorted Array
- leetcode27. Remove Element
- leetcode58. Length of Last Word
- leetcode66. Plus One
- leetcode67. Add Binary
- leetcode80. Remove Duplicates from Sorted Array II
- leetcode 88. Merge Sorted Array
- leetcode101. Symmetric Tree
- leetcode104. Maximum Depth of Binary Tree
- leetcode110. Balanced Binary Tree
- leetcode118. Pascal's Triangle
- leetcode119. Pascal's Triangle II
- leetcode121. Best Time to Buy and Sell Stock
- leetcode125. Valid Palindrome
- leetcode136. Single Number
- leetcode144. Binary Tree Preorder Traversal
- leetcode145. Binary Tree Postorder Traversal
- leetcode155. Min Stack
- leetcode169. Majority Element
- leetcode171. Excel Sheet Column Number
- leetcode172. Factorial Trailing Zeroes
- leetcode189. Rotate Array
- leetcode191. Number of 1 Bits
- leetcode202. Happy Number
- leetcode203. Remove Linked List Elements
- leetcode204. Count Primes
- leetcode225. Implement Stack using Queues
- leetcode226. Invert Binary Tree
- leetcode231. Power of Two
- leetcode232. Implement Queue using Stacks
- leetcode234. Palindrome Linked List
- leetcode237. Delete Node in a Linked List
- leetcode242. Valid Anagram
- leetcode258. Add Digits
- leetcode263. Ugly Number
- leetcode283. Move Zeroes
- leetcode292. Nim Game
- leetcode344. Reverse String
- leetcode371. Sum of Two Integers
- leetcode804. Unique Morse Code Words
- Leetcode JS 实现
- Js-leetcode 1. Two Sum
- Js-leetcode 2. Add Two Numbers
- Js-leetcode 10. Regular Expression Matching
- Js-leetcode 14. Longest Common Prefix
- Js-leetcode 17.Letter Combinations of a Phone Number
- Js-leetcode 20. Valid Parentheses
- Js-leetcode 27. Remove Element
- Js-leetcode 30. Substring with Concatenation of All Words
- Js-leetcode 35. Search Insert Position
- Js-leetcode 41. First Missing Positive
- Js-leetcode 48. Rotate Image
- Js-leetcode 53. Maximum Subarray
- Js-leetcode 54. Spiral Matrix
- Js-leetcode 73. Set Matrix Zeroes
- Js-leetcode 75. Sort Colors
- Js-leetcode 85. Maximal Rectangle
- Js-leetcode 89. Gray Code
- Js-leetcode 93.Restore IP Addresses
- Js-leetcode 98. Validate Binary Search Tree
- Js-leetcode 104. Maximum Depth of Binary Tree
- Js-leetcode 118. Pascal's Triangle
- Js-leetcode 121. Best Time to Buy and Sell Stock
- Js-leetcode 136. Single Number
- Js-leetcode 164. Maximum Gap
- Js-leetcode 189. Rotate Array
- Js-leetcode 215. Kth Largest Element in an Array
- Js-leetcode 217. Contains Duplicate
- Js-leetcode 258. Add Digits
- Js-leetcode 263.Ugly Number
- Js-leetcode 283. Move Zeroes
- Js-leetcode 326. Power of Three
- Js-leetcode 434. Number of Segments in a String
- Js-leetcode 459. Repeated Substring Pattern
- Js-leetcode 507. Perfect Number
- Js-leetcode 537. Complex Number Multiplication
- Js-leetcode 541. Reverse String II
- Js-leetcode 551. Student Attendance Record I
- Js-leetcode 557. Reverse Words in a String III
- Js-leetcode 605. Can Place Flowers
- Js-leetcode 606. Construct String from Binary Tree
- Js-leetcode 621. Task Scheduler
- Js-leetcode 622. Design Circular Queue
- Js-leetcode 633. Sum of Square Numbers
- Js-leetcode 657. Robot Return to Origin
- Js-leetcode 682. Baseball Game
- Js-leetcode 686. Repeated String Match
- Js-leetcode 696. Count Binary Substrings
- Js-leetcode 709.To Lower Case.md
- Js-leetcode 771 Jewels and Stones
- Js-leetcode 804.Unique Morse Code Words
- Js-leetcode 867. Transpose Matrix
- Js-leetcode 905. Sort Array By Parity
- Js-leetcode 914. X of a Kind in a Deck of Cards
- Js-leetcode 922. Sort Array By Parity II
- Js-leetcode 1221. Split a String in Balanced Strings
- Js-leetcode 3. Longest substring without repeating characters
- document
- 刘宇波老师--基础知识
- 算法与数据结构
- 数据机构与算法介绍
- 二叉树
- 堆
- 链表
- 队列
- 并查集
- 最小生成树
- 图论
- 最短路径
- 总结
- 玩转数据结构
- 介绍
- 数组
- 栈和队列
- 玩转链表
- 链表与递归
- 二分搜索树
- 集合与映射
- 第8章 优先队列和堆
- 第9章 线段树
- 第10章 Trie
- 玩转算法面试 leetcode题库分门别类详细解析
- 第一章 介绍
- 第二章 时间复杂度和空间复杂度
- 第三章
- 深度实战玩转算法
- JS数据结构与算法
- 第一章.介绍
- 第二章.字符串
- 第三章.数组
- 第四章
- 第五章.排序
- 第六章.递归
- 第七章.栈
- 第八章.队列
- 第九章.链表
- 第十章.矩阵
- 第十一章.二叉树
- 第十二章.堆
- 极客时间
- 第01课丨数据结构与算法总览
- 第02课丨训练准备和复杂度分析
- 第03课丨数组、链表、跳表
- 第04课丨栈、队列、优先队列、双端队列
- 算法基础
- 哈希表(散列表)
- 异或运算
- leetcode
- KD树(K-Dimension Tree)
- Rtree
- B站-左神
- 一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解
- 简单的排序算法
- 认识Nlog(N)的算法
- 第三节:桶排序及排序算法总结
- 左程云算法课程
- 1 认识复杂度、对数器、二分法与异或运算
- 2 链表结构、栈、队列、递归行为、哈希表和有序表
- 10道BAT必问算法面试题
- 超级水王问题
- 78节
- 04链表