题目

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)

原题链接(点我)

解题思路

给一个字符串,将其转换成IP地址。

这个题能够理解成,将字符串分割成若干个小块,将当中合法的(数值在0-255)组合起来,拼接成一个合法的IP地址。讲到这也就看出来了这还是一个组合问题。对于组合问题。算法都是结构性的递归套循环,看例如以下代码。

代码实现

  1. class Solution {
  2. public:
  3. vector<string> restoreIpAddresses(string s) {
  4. vector<string> ret;
  5. helper(0, 0, s, string(), ret);
  6. return ret;
  7. }
  8. void helper(int k, int start, const string& s, string part, vector<string> & ret){
  9. if(k>4) return;
  10. if(k==4){
  11. if(start == s.size()){
  12. ret.push_back(part);
  13. }
  14. return;
  15. }
  16. int key=0;
  17. if(k>0)
  18. part += ".";
  19. for(int i=start; i<s.size(); ++i){
  20. if(i>start && s[start] == '0') break;
  21. key = 10*key + s[i]-'0';
  22. if(key > 255) return;
  23. string temp = part;
  24. temp += s.substr(start, i-start+1);
  25. helper(k+1, i+1, s, temp, ret);
  26. }
  27. }
  28. };
假设你认为本篇对你有收获。请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30027655
)

[LeetCode] Restore IP Address [28]的更多相关文章

  1. LeetCode:Restore IP Address

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

  2. [Leetcode] restore ip address 存储IP地址

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

  3. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  4. [LeetCode] Restore IP Addresses 复原IP地址

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

  5. [LintCode] Restore IP Address 复原IP地址

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

  6. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  7. LeetCode——Restore IP Addresses

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

  8. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  9. [LeetCode] Restore IP Addresses 回溯

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

随机推荐

  1. Aspose.Cells 基础用法

    最近使用Aspose.Cells做Excel,在怎么添加批注和添加内部导航链接上耗费了一些时间,最后在官网上找到相关用法,记录一下. 代码不用过多介绍,看看即可明白. 测试代码下载 Workbook ...

  2. Visual Studio Code 好用的 source code editor

    short cut https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf go to definition : F1 ...

  3. JSON的相关内容

    包括: JSON概述, JSON语法规则, 使用JSON(JSON对象,JSON数组,JSON文档与对象的转换) JSON应用 额外内容(PHP输出JSON数据,解析JSON字符串,客户端如何使用服务 ...

  4. 6.安装和配置OpenStack图片服务组件

    安装和配置图片服务组件 这里是安装在控制器上 安装和配置图片服务组件 yum install –y openstack-glance python-glanceclient 编辑/etc/glance ...

  5. python的递归算法学习(2):具体实现:斐波那契和其中的陷阱

    1.斐波那契 什么是斐波那契,斐波那契额就是一个序列的整数的排序,其定义如下: Fn = Fn-1 + Fn-2 with F0 = 0 and F1 = 1 也就是,0,1,1,2,3,5,8,13 ...

  6. hdu 5084(矩阵操作)

    HeHe Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. AC日记——Sliding Window poj 2823

    2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> ...

  9. HDU 2546.饭卡-动态规划0-1背包

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  10. HDU 1203 【01背包/小数/概率DP】

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...