Java for LeetCode 093 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)

解题思路:

使用循环即可解决,JAVA实现如下:

	static public List<String> restoreIpAddresses(String s) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 3; i++)
			for (int j = i + 2; j < i + 5&&j<=s.length()-2; j++)
				for (int k = j + 1; k < j + 4&&k<=s.length()-1; k++){
						if (isValid(s.substring(0, i + 1))
								&& isValid(s.substring(i + 1, j))
								&& isValid(s.substring(j, k))
								&& isValid(s.substring(k, s.length())))
							list.add(s.substring(0, i + 1) + "."
									+ s.substring(i + 1, j) + "."
									+ s.substring(j, k) + "."
									+ s.substring(k, s.length()));
					}
		return list;
	}
		public static boolean isValid(String s) {
		if (s.length()>3)
			return false;
		int num = Integer.parseInt(s);
		return num <= 255 && (num + "").equals(s);
	}
时间: 2025-01-04 22:33:57

Java for LeetCode 093 Restore IP Addresses的相关文章

【leetcode】Restore IP Addresses

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 mat

LeetCode: 93. 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) 给一串数字字符串,返回所有可能的

[LeetCode]46. 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) Subscribe to se

leetcode No93. Restore IP Addresses

Question: 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地

【leetcode】Restore IP Addresses (middle)

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) 思路:回溯法 解向量X={str

【LeetCode】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) [解析] 题意:把一

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

LeetCode 93. Restore IP Addresses 20170705 部分之前做了没写的题目

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) 题目大意:给出一串数字组成的字符

LeetCode OJ - Restore IP Addresses

这道题采用穷举法. 1 /** 2 * Given a string containing only digits, 3 * restore it by returning all possible valid IP address combinations. 4 * 采用穷举法 5 * @param s 6 * @return 7 */ 8 public ArrayList<String> restoreIpAddresses(String s) { 9 ArrayList<Strin