【模拟】力扣38(Java)

题目
【模拟】力扣38(Java)_第1张图片

class Solution {
    public String countAndSay(int n) {
        String ret = "1";
        for(int i=1;i<n;i++)//解释n-1次ret
        {
        	StringBuffer tmp = new StringBuffer();

        	int len = ret.length();
        	for(int left = 0,right = 0;right<len;)
        	{
                //双指针
        		while(right < len && ret.charAt(left) == ret.charAt(right))
        			right++;
        		tmp.append(Integer.toString(right - left));//个数
        		tmp.append(ret.charAt(left));//字符
        		left = right;
        	}
        	ret = tmp.toString();
        }
        return ret;
    }
}

你可能感兴趣的:(模拟,leetcode,java,算法)