Easy understanding Java code


  • 0
    V
    public String countAndSay(int n) {
        String returnString = "1";
        for (int i = 2; i <= n; i++) {
            returnString = next(returnString);
        }
        return returnString;
    }
    
    public String next(String pre){
        char current = 'x';
        String next = "";
        int count = 0;
        for (int i = 0; i < pre.length(); i++) {
            if (current!= pre.charAt(i)){
                if (current!='x') next = next + count+"" + String.valueOf(current);
                current = pre.charAt(i);
                count =0;
            }
            count++;
        }
        next = next + count+"" + String.valueOf(current);
        return next;
    }

Log in to reply
 

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