Accepted Java Solution, easy understand.


  • 0
    Y
    public class Solution {
        public String countAndSay(int n) {
            if (n == 0) {
                return "";
            }
            if (n == 1) {
                return "1";
            }
            String s = countAndSay(n - 1);
            String res = "";
            for (int i = 0; i < s.length(); i++){
                int count = 1;
                char temp = s.charAt(i);
                while (i + 1 < s.length() && s.charAt(i + 1) == temp) {
                    count++;
                    i++;
                }
                res = res + count + temp;
            }
            return res;
        }
    }
    

  • 0
    L

    @yufengyuanx said in Accepted Java Solution, easy understand.:

    i + 1 < s.length() && s.charAt(i + 1) == temp) {
    count++;
    can u explain "while (i + 1 < s.length() && s.charAt(i + 1) == temp)"


Log in to reply
 

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