博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
93. Restore IP Addresses
阅读量:4952 次
发布时间:2019-06-12

本文共 1060 字,大约阅读时间需要 3 分钟。

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"]

 

AC code:

class Solution {public:    vector
restoreIpAddresses(string s) { vector
res; helper(0, s, res, "", 0); return res; } void helper(int count, string s, vector
& res, string ret, int index) { if (count > 4) return; if (count == 4 && index == s.length()) res.push_back(ret); for (int i = 1; i < 4; i++) { if (index+i > s.length()) break; string temp = s.substr(index, i); if(temp[0] == '0' && temp.length() > 1 || atoi(temp.c_str()) > 255) continue; string next = ret + temp; helper(count+1, s, res, next+(count == 3 ? "" : "."), index+i); } }};

Runtime: 4 ms, faster than 23.68% of C++ online submissions for Restore IP Addresses.

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/9867423.html

你可能感兴趣的文章
Android SDK环境变量配置
查看>>
VM10虚拟机安装图解
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
JZOJ 4.1 B组 俄罗斯方块
查看>>
HDU6409 没有兄弟的舞会
查看>>
2018 Multi-University Training Contest 10 - TeaTree
查看>>
HDU6205 card card card
查看>>
2018 Multi-University Training Contest 10 - Count
查看>>
HDU6198 number number number
查看>>
HDU6438 Buy and Resell
查看>>
HDU6446 Tree and Permutation
查看>>
HDU6201 transaction transaction transaction
查看>>
HDU6203 ping ping ping
查看>>
前端小笔记
查看>>
《人人都是产品经理》书籍目录
查看>>
Netsharp系列文章目录结构
查看>>
如何在git bash中运行mysql
查看>>
OO第三阶段总结
查看>>
构建之法阅读笔记02
查看>>