leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法

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地址只有3个点,根据点的个数判断合法不合法(也即字符串长度只能比点的个数多,且小于3*(k+1),k为点的个数)。

如果最后不需要添加点,则字段值小于256,则合法,添加结果集。

代码如下:

public class Solution {
    List<String> list;
    public List<String> restoreIpAddresses(String s) {
    	list = new ArrayList<String>();
    	/**
    	 * 初始传入的字符串长度只能为4-12
    	 */
        addDot(s,"",3);

        return list;
    }
    /**
     *
     * @param s 需要处理的字符串
     * @param result 装载结果的字符串
     * @param k 当前还有几个"."
     */
    private void addDot(String s,String result, int k){

    	//传入字符串必须合法
    	if(s.length() <= k){
    		return;
    	}
    	if(s.length() > 3*(k+1)){
    		return;
    	}
    	if(k == 0){//最后一段,判断小于256
    		if((s.charAt(0) == '0' && s.length() > 1 ) || Integer.parseInt(s) >= 256)
    			return;
    		list.add(result + "." + s);
    		return;
    	}
    	//遍历前面3位,如果小于256,则参与递归
    	for(int i = 1; i <= s.length(); i++){
    		String temp = s.substring(0,i);//当前段小于256,表示合法,可以继续
    		if(Integer.parseInt(temp) < 256){
    			String str = s.substring(i);
    			//分情况,如果result为空,则前面不加"."
    			if(result.length() > 0){
    				temp = result + "." + temp;
    			}
    			addDot(str,temp, k-1);
    			//如果第一个数字为0,则不再继续循环
    			if(s.charAt(0) == '0'){
    				break;
    			}
    		}else{
    			break;
    		}
    	}
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-08 00:17:45

leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法的相关文章

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" o

leetCode 62.Unique Paths (唯一路径) 解题思路和方法

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

leetCode 40.Combination Sum II(组合总和II) 解题思路和方法

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

leetCode 89.Gray Code (格雷码) 解题思路和方法

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Fo

leetCode 90.Subsets II(子集II) 解题思路和方法

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a soluti

leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 思路:此题的意思是给一个为0或1的矩阵,求所有为1组成的最大矩阵的面积. 此题能够巧妙转化为求最大直方图面积的问题. public class Solution { //其思想是将每一列的1逐行相加,遇0为0.遇1相加 //然后转化为求每一行的最大直方图面积的求解

leetCode 76.Minimum Window Substring(最小窗口子串) 解题思路和方法

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo