您的位置:首页 > 产品设计 > UI/UE

leetcode Question 83: Restore IP Addresses

2015-01-24 02:57 344 查看
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)

Analysis:

This problem can be viewed as a DP problem. There needed 3 dots to divide the string, and make sure the IP address is valid:
less than or equal to 255, greater or equal to 0, and note that, "0X" or "00X" is not valid.

For the DP, the length of each part is from 1 to 3. We use a vector<string> to store each part, and cut the string every
time. Details see the code.

Note that "atoi" is for c-string, <string> need to convert to cstring by str.c_str();


Code(Updated 201309):

?
Code(old version):

?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: