先将字符串第一个字符作为初始条件放在外面,初始化记数为1,然后开始循环,从字符串索引1开始,判断第0位和第1位字符是否相等,相等count就加1,继续循环,如果不相等,拼接字符串,并将count还原为1,判断对象由第0位字符换成第1位字符,继续循环。 publicStringcountAndSay2(int n) {if(n >30|| n <0) {return...
Leetcode 题目整理-8 Count and Say 38. 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" ...
public StringcountAndSay2(int n){if(n>30||n<0){return"";}if(n==1){return"1";}String previous="1";for(int j=1;j<n;j++){String str="";char c=previous.charAt(0);int count=1;for(int i=1;i<previous.length();i++){char cc=previous.charAt(i);if(cc==c){count++;}else{...
Question 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 ...
天天算法 | Easy | 11. 计数和描述:Count and Say Mark 中国科学院大学 计算机系统结构硕士15 人赞同了该文章 每天来一道,面试不卡壳,今天是天天算法陪你成长的第11天 本题可在LeetCode上OJ,链接: Count and Say 题目描述: The count-and-say sequence is the sequence of integers with the first...
11is read off as"two 1s"or21. 21is read off as"one2, thenone1"or1211. Given an integern, generate thenthsequence. Note: The sequence of integers will be represented as a string。 原题链接:https://oj.leetcode.com/problems/count-and-say/ ...
*/public StringcountAndSay(int n){// Write your code hereString oldString="1";while(--n>0){StringBuilder sb=newStringBuilder();char[]oldChars=oldString.toCharArray();for(int i=0;i<oldChars.length;i++){int count=1;while((i+1)<oldChars.length&&oldChars[i]==oldChars[i+1]){count...
publicStringcountAndSay(intn){if(n==1){return"1";}char[]re=countAndSay(n-1).toCharArray();intcount=0;charcur=re[0];StringBuildersb=newStringBuilder();for(inti=0;i<re.length;i++){if(cur!=re[i]){sb.append(count);sb.append(cur);cur=re[i];count=1;}else{count++;}}sb.append...
countAndSay(1) = "1" countAndSay(n)是对countAndSay(n-1)的描述,然后转换成另一个数字字符串。 前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 第一项是数字 1 描述前一项,这个数是1即 “一个 1 ”,记作"11"描述前一项,这个数是11即 “二个 1 ” ,记作"21"描述前一项,这个数是...
implSolution{pubfncount_and_say(n:i32)->String{ifn==1{return"1".to_string();}letprev=Self::count_and_say(n-1).chars().collect::<Vec<_>>();letmutret=String::new();letmutcount=1;foriin0..prev.len(){ifi+1<prev.len()&&prev[i+1]==prev[i]{count+=1;}else{ret.push_str(...