JAVA 4ms soultion


  • 0
    O
    public class Solution {
        public static List<String> result;
    
        public static List<String> restoreIpAddresses(String s) {
            result = new ArrayList<String>();
            helper(s.toCharArray(),4,"",s.length());
            return result;
        }
    
    
        public static void helper(char[] chars, int seg, String pre, int L) {
            int length = chars.length;
            if(length == 0 || length > seg * 3 || length < seg){
                return;
            }
    
            for(int j = 1; j <= 3; j++){
                if(j <= length) {
                    char[] tmp = Arrays.copyOfRange(chars, j, length);
                    String s = String.valueOf(Arrays.copyOfRange(chars, 0, j));
                    if(j != 1 && chars[0] == '0'){
                        continue;
                    }
                    if (Integer.parseInt(s) <= 255 && Integer.parseInt(s) >= 0) {
                        if (s.length() == L && seg == 1) {
                            result.add(pre + s);
                            return;
                        }
                        if (pre.length() != 0)
                            helper(tmp, seg - 1, pre + s + ".", L - s.length());
                        else
                            helper(tmp, seg - 1, s + ".", L - s.length());
                    }
                }
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.