Java Simple Solution


  • 2
    V
    public class Solution {
        public String countAndSay(int n) {
            String curr = "1";
            for (int i=2; i<=n; i++) {
                String temp = "";
                for (int j = 0; j<curr.length(); j++) {
                    int count = 1;
                    char pres = curr.charAt(j);
                    while (j+count<curr.length() && pres == curr.charAt(j+count)) 
                        count++;
                    temp += String.valueOf(count)+pres;
                    j+=count-1;
                }
                curr = temp;
            }
            return curr;
        }
    }

Log in to reply
 

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