Is it the shortest recursive Java?


  • 1
    A
    private List<String> res = new ArrayList<>();
    
    public List<String> restoreIpAddresses(String s) {
        restore(s, 0, new String[4], 0);
        return res;
    }
    
    private void restore(String s, int i, String[] ip, int j) {
        if (j >= 4 && i != s.length()) return;
        if (j == 4 && i == s.length()) res.add(String.join(".", ip));
        for (int k = i + 1; k <= s.length(); k++) {
            String sub = s.substring(i,k);
            if (sub.charAt(0) == '0' && sub.length() > 1) return;
            if (Integer.parseInt(sub) > 255) return;
            ip[j] = sub; 
            restore(s, k, ip, j+1);
        }
    }

Log in to reply
 

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