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 ...
随机推荐
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- silverlight 跳转指定的aspx页面
1.在xaml.cs中直接访问.并传递参数 System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(HtmlPage.Document.Docu ...
- php获取服务器时间的代码
php获取服务器时间的代码. 用php的date函数即可来获取服务器上的时间: <?php //将时区设置为中国 date_default_timezone_set("PRC&quo ...
- JQuery 获取json数据$.getJSON方法的实例代码
这篇文章介绍了JQuery 获取json数据$.getJSON方法的实例代码,有需要的朋友可以参考一下 前台: function SelectProject() { var a = new Array ...
- PHPcms 摘要
一 常量 /** * 主要定义了路径常量,项目中经常用到 **/ define('PHPCMS_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);// 项目 ...
- 仿UC天气下拉和微信下拉眼睛头部淡入淡出--第三方开源--PullLayout
Android-PullLayout是github上的一个第三方开源项目,该项目主页是:https://github.com/BlueMor/Android-PullLayout 原作者项目意图实现类 ...
- php win主机下实现ISAPI_Rewrite伪静态
有的win主机iss不支持 .htaccess 文件, 我在这里指的不是本地 在本地的话用apmserv服务器可以用.htaccess 文件,用apmserv服务器环境配置伪静态可以看 php 伪静态 ...
- rsync参数详解、利用ssh、rsync 实现数据的定时同步
rsync 简介 rsync(remote synchronize)是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文 件.也可以使用 rsync 同步本
- Python的类实例方法,类方法,类静态方法
以下面的类定义为例: # coding:utf-8 class A: count = 0 def __init__(self, inst_name): self.inst_name = inst_na ...
- 每日一“酷”之copy
Copy – 复制对象 作用:提供一些函数,可以使用浅副本或深副本语义复制对象. copy模块包括两个函数copy()和deepcopy(),用于复制现有的对象 1. 浅副本 copy()创建的浅副 ...