Leetcode(93): Restore IP Addresses
发布日期:2021-05-15 10:07:10 浏览次数:17 分类:博客文章

本文共 1065 字,大约阅读时间需要 3 分钟。

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)

 

1 public class Solution { 2     public List
restoreIpAddresses(String s) { 3 List
res=new ArrayList
(); 4 if(s.length()<4||s.length()>12) return res; 5 dfs(s,"",res,0); 6 return res; 7 } 8 public static void dfs(String s,String temp,List
list,int count){ 9 if(count==3&&isValid(s)){10 list.add(temp+s);11 return;12 }13 for(int i=1;i<4&&i
0;25 }26 public static void main(String[] args) {27 // TODO Auto-generated method stub28 Scanner sc=new Scanner(System.in);29 solution sl=new solution();30 31 List
list=sl.restoreIpAddresses(sc.next());32 33 for(String i:list){34 System.out.println(i);35 }36 }37 38 }

 

上一篇:Java使用BigDecimal解决精确计算的问题
下一篇:LeetCode(144):Binary Tree Preorder Traversal

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月14日 20时26分55秒