码迷,mamicode.com
首页 > 其他好文 > 详细

38. Count and Say

时间:2018-03-15 13:21:14      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:turn   created   rup   har   markdown   main   uil   clear   else   

原题链接:https://leetcode.com/problems/count-and-say/description/
奶奶的,这道题本身题意都不好读懂,然后即使明白了也还是没有写出解答来,所以最后还是无耻的抄袭了讨论区的答案!
首先要读懂题意:https://www.2cto.com/kf/201507/416783.html
然后是解答:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    public static void main(String[] args) throws InterruptedException {
        Solution s = new Solution();
        System.out.println(s.countAndSay(6)); // 312211
    }

    public String countAndSay(int n) {
        String s = "1";
        for (int i = 1; i < n; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 1, count = 1; j <= s.length(); j++) {
                if (j == s.length() || s.charAt(j - 1) != s.charAt(j)) {
                    sb.append(count);
                    sb.append(s.charAt(j - 1));
                    count = 1;
                } else {
                    count++;
                }
            }
            s = sb.toString();
        }
        return s;
    }

}

38. Count and Say

标签:turn   created   rup   har   markdown   main   uil   clear   else   

原文地址:https://www.cnblogs.com/optor/p/8572687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!