“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.” Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
public class { public List<String> restoreIpAddresses (String s) { StringBuilder str = new StringBuilder(s); List<String> res = new ArrayList<>(); if (s.length()<4 || s.length()>12 ) return res; helper(res, s, new String(), 0 , 1 ); return res; } private void helper (List<String> res, String s, String str, int pos, int depth) { if (depth > 3 ) { String seg = s.substring(pos); if (isValid(seg)) { str = str + seg; res.add(str); } return ; } for (int i = 1 ; i <= 3 && pos + i < s.length(); i++) { String seg = s.substring(pos, pos + i); if (isValid(seg)) { helper(res, s, str + seg + "." , pos + i, depth + 1 ); } } } private boolean isValid (String seg) { if (seg.charAt(0 ) == '0' && seg.length()>1 ) return false ; int ipNum = Integer.valueOf(seg); if (0 <= ipNum && ipNum <= 255 ) return true ; return false ; } public static void main (String[] args) { new leetcode93().restoreIpAddresses("010010" ); } }
近期评论