Java solution


  • 0
    W
    public class Solution {
        public String countAndSay(int n) {
            String say = "1";
            for (int j = 1; j < n; j++) {
                char pre = say.charAt(0);
                int count = 1;
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < say.length(); i++) {
                    char cur = say.charAt(i);
                    if (cur == pre) {
                        count++;
                    } else {
                        sb.append(count).append(pre);
                        count = 1;
                    }
                    pre = cur;
                }
                sb.append(count).append(pre);
                say = sb.toString();
            }
            return say;
        }
    }

Log in to reply
 

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