国产探花免费观看_亚洲丰满少妇自慰呻吟_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;    }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金川县| 宜良县| 焦作市| 铁力市| 革吉县| 苏尼特右旗| 青田县| 禄丰县| 南通市| 奉节县| 敖汉旗| 新巴尔虎右旗| 徐闻县| 石嘴山市| 绍兴县| 乌兰浩特市| 上蔡县| 郯城县| 彰武县| 吉林市| 伊宁市| 库尔勒市| 仁怀市| 丹东市| 简阳市| 望江县| 南江县| 柏乡县| 城口县| 大荔县| 浦城县| 岗巴县| 绥棱县| 梓潼县| 阳东县| 汕尾市| 遂溪县| 瓦房店市| 南雄市| 宜城市| 青州市|