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)
Solution:
注意这里if(tmp < 256 && tmp.toString().equals(s.substring(pos, pos + j)))
如果不加tmp.toString().equals(s.substring(pos, pos + j)), 则可能会出现001 这种情况, 即高位为0.
public class Solution {
ArrayList<String> result = null;
public ArrayList<String> restoreIpAddresses(String s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
result = new ArrayList<String>();
getAddress(s, 0, 4, new StringBuffer());
return result;
}
public void getAddress(String s, int pos, int num, StringBuffer sb){
if(num == 0 || pos == s.length()){
if(pos == s.length() && num == 0){
result.add(sb.substring(0, sb.length() - 1));
}
return;
}
for(int j = 1; j < 4 && pos + j <= s.length(); j ++){
Integer tmp = Integer.valueOf(s.substring(pos, pos + j));
if(tmp < 256 && tmp.toString().equals(s.substring(pos, pos + j))){
sb.append(s.substring(pos, pos + j));
sb.append(".");
getAddress(s, pos + j, num - 1, sb);
sb.delete(sb.length() - j - 1, sb.length());
}
}
}
}
Restore IP Addresses的更多相关文章
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- javaweb毕业设计
javaweb毕业设计,管理系统设计,Strut2项目,Springmvc项目,javaweb项目学习. 都是可以运行的,(配数据库和论文),都能提供技术咨询,可以修改.欢迎+扣扣396058587咨 ...
- IOS,发短信,发邮件,打电话
今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...
- 使用原生Sql查询实现按分类推送最新文章到首页
一般在网站的首页都会有网站最新文章的推送,而这些文章又属于不同的分类.如果某个分类的文章突然集中在一个时间段发布,那么就会造成首页上所有文章都是该分类的文章,其他的文章分类就变成不可见的了.所以,我希 ...
- 【风马一族_xml】xml的基本讲解笔记
xml是如何保存数据的 在xml语言中,它允许用户自定义标签.每个标签用于描述一段数据; 一个标签可以分为开始标签和结束标签,在开始标签和结束标签之间又可以嵌套其它标签,利用标签间的嵌套其它标签,利用 ...
- ubuntu OPENCV移植
Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS 这是转载国外一篇文章 移植PC上的OPENCV http://www.samontab.com/web/20 ...
- JavaIO和JavaNIO
BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...
- PHP中include和require绝对路径、相对路径问题
在写PHP程序时,经常要用到include或require包含其他文件,但是各文件里包含的文件多了之后,就会产生路径问题. 如下目录: <web>(网站根目录) ├<A>文件夹 ...
- Python开发【第一篇】Python基础之反射
反射 反射的作用:反射得作用是提高代码可读行. __import__导入模块和import导入模块的区别: __import__导入模块是通过字符串进行导入. import是常用得导入模块方法. 扩展 ...
- 【转载】Powershell设置世纪互联Office365嵌套组发送权限
Start-Transcript ".\Set-GroupSendPermisionLog.txt" -Force function Get-DLMemberRecurse { $ ...
- Sql Server 常用自定义函数
-- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...