Advertisement
Guest User

Untitled

a guest
May 25th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public class Solution {
  2. public static ArrayList<String> restoreIpAddresses(String s) {
  3. ArrayList<String> list = new ArrayList<String>();
  4. int l = s.length();
  5. for (int i = 1; i <= 3; i++) {
  6. if (l - i > 9) continue;
  7. for (int j = i + 1; j <= i + 3; j++) {
  8. if (l - j > 6) continue;
  9. for (int k = j + 1; k <= j + 3; k++) {
  10. if (l - k > 3 || l - k < 1) continue;
  11. String s1 = s.substring(0, i);
  12. String s2 = s.substring(i, j);
  13. String s3 = s.substring(j, k);
  14. String s4 = s.substring(k, l);
  15. if (s1.length() > 1 && s1.charAt(0) == '0') continue;
  16. if (s2.length() > 1 && s2.charAt(0) == '0') continue;
  17. if (s3.length() > 1 && s3.charAt(0) == '0') continue;
  18. if (s4.length() > 1 && s4.charAt(0) == '0') continue;
  19. int n1 = Integer.parseInt(s1);
  20. int n2 = Integer.parseInt(s2);
  21. int n3 = Integer.parseInt(s3);
  22. int n4 = Integer.parseInt(s4);
  23. if (n1 > 255 || n2 > 255 || n3 > 255 || n4 > 255) continue;
  24. String ip = s1 + "." + s2 + "." + s3 + "." + s4;
  25. list.add(ip);
  26. }
  27. }
  28. }
  29. return list;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement