首页 > 代码库 > Restore IP Addresses
Restore IP Addresses
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
答案
public class Solution { int LEN; public List<String> getRemainingString(String s,int position,int index){ int len=0; List<String> result=new LinkedList<String>(); String subString; if(index==3) { subString=s.substring(position); int p=Integer.parseInt(subString); if(p>=0&&p<=255){ if(!(subString.length()>1&&subString.startsWith("0"))){ result.add(subString); } } return result; } int maxLEN=Math.min(3,LEN-position-(3-index)); for(len=1;len<=maxLEN;len++){ subString=s.substring(position,position+len); int p=Integer.parseInt(subString); if(p>=0&&p<=255){ if(!(subString.length()>1&&subString.startsWith("0"))){ result.add(subString); } } } return result; } public List<String> parseIP(String s,int position,int index) { List<String> result=new LinkedList<String>(); List<String>p=getRemainingString(s,position,index); if(index==3){ return p; } for(String current:p) { List<String>t=parseIP(s,position+current.length(),index+1); for(String next:t) { result.add(current+"."+next); } } return result; } public List<String> restoreIpAddresses(String s) { if(s==null||s.length()<4||s.length()>12){ return new LinkedList<String>(); } LEN=s.length(); return parseIP(s,0,0); } }
Restore IP Addresses
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。