国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Count and Say

2019-11-15 01:15:51
字體:
來源:轉載
供稿:網友
Count and SayCount and Say

https://leetcode.com/PRoblems/count-and-say/

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.Given an integer n, generate the nth sequence.Note: The sequence of integers will be represented as a string.

算法思想:

1) 可以根據當前的這個string,來計算下一個string,如“1211”,得到“one 1 one 2 two 1”,即"111221"

程序代碼:
public class Solution {    public String next(String s) {        int length = s.length();        char pre = s.charAt(0);        int count = 1;        StringBuilder sb = new StringBuilder();        for (int i = 1; i < length; i++) {            if (s.charAt(i) == pre) {                count++;            } else {                sb.append(count);                sb.append(pre);                count = 1;                pre = s.charAt(i);            }        }        sb.append(count);        sb.append(pre);                return sb.toString();    }        public String countAndSay(int n) {        if (n <= 0) {            return "";        }                String cas = "1";        for (int i = 2; i <= n; i++) {            cas = next(cas);        }        return cas;    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大名县| 舟曲县| 静海县| 百色市| 洮南市| 灵宝市| 资阳市| 阿鲁科尔沁旗| 巴青县| 黑山县| 莱西市| 黄陵县| 阿拉善右旗| 革吉县| 张家界市| 德清县| 东平县| 五莲县| 康乐县| 龙里县| 夏邑县| 准格尔旗| 怀柔区| 雅安市| 高碑店市| 万山特区| 阿拉善盟| 和林格尔县| 揭西县| 成安县| 兴和县| 灵宝市| 宜都市| 个旧市| 台南市| 灌云县| 彰武县| 安岳县| 蛟河市| 若尔盖县| 陆川县|