093. Restore IP Addresses

 1 class Solution {
 2 public:
 3     vector<string> restoreIpAddresses(string s) {
 4         vector<string> res;
 5         string current;
 6         if (s.size() > 12 || s.size() < 4) return res;
 7         else {
 8             checkIp(s, res, current, 0, 1);
 9             return res;
10         }
11     }
12 private:
13     void checkIp(string s, vector<string>& res, string& current, int startPos, int step)
14     {
15         if (startPos > s.size() - 1) return;
16         if (step == 4) {
17             if (startPos < s.size() - 3) return;
18             else {
19                 if (isValid(s, startPos, s.size() - 1)) {
20                     current.append(s, startPos, s.size() - startPos);
21                     res.push_back(current);
22                     current.erase(current.size() + startPos - s.size(), s.size() - startPos);
23                     return;
24                 }
25             }
26         }
27         else {
28             for (int i = 0; i < 3 && startPos + i < s.size(); ++i) {
29                 if (isValid(s, startPos, startPos + i)) {
30                     current.append(s, startPos, i + 1);
31                     current.push_back(‘.‘);
32                     checkIp(s, res, current, startPos + i + 1, step + 1);
33                     current.pop_back();
34                     current.erase(current.size() - i - 1, i + 1);
35                 }
36             }
37         }
38     }
39
40     bool isValid(string s, int left, int right) {
41         if (s[left] == ‘0‘ && right > left) return false; // 这里需要判断如果以0开头,则只能有一位0
42         int num = 0;
43         for (int i = left; i <= right; ++i) {
44             num *= 10;
45             num += s[i] - ‘0‘;
46         }
47         if (num >= 0 && num < 256) return true;
48         else return false;
49     }
50 };
时间: 2024-10-19 09:23:38

093. Restore IP Addresses的相关文章

Java for LeetCode 093 Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 解题思路: 使用循环即可解决,

LeetCode: Restore IP Addresses [093]

[题目] Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) [题意] 给定一个字

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[LeetCode]Restore IP Addresses

题目:Restore IP Addresses 将数字串转换成合法的IP地址,返回所有可能的情况. 思路: 移动3个地址分隔符".",将地址换分成四份,检查是否合法: 注意不能有0开头的地址段(它是非法的),且不用将开头的0去掉. 中间的点从第一个点的后面开始向尾部移动,当第一个点与第二个点的距离大于3时,第一个点向后移动一步,第二个点还原到当前第一个点的后面: 当第一个点移动到最后一个点的前2个位置,则,最后一个点向前移动一步,第一个点和第二个点还原: 最后一个点到串尾距离大于3则退

Leetcode题解(2):L93/Restore IP Addresses

L93: Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does n

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode】Restore IP Addresses

Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not mat

Restore IP Addresses leetcode java

题目: Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解: 利用循环递归解

【LeetCode】93. Restore IP Addresses 【面试题】

Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not mat