}stringcountAndSay(intn){stringres ="1";while(--n) { res = read(res); }returnres; } 解法2:调整了解法1中read函数的处理逻辑。同样从左往右遍历一遍,若与后一个数相同,计数值count加1;若不同,将计数值和当前位置数值插入字符串,同时重置计数值。为了防止最后一位比较时出现错误,在原字符串末尾添加...
publicStringcountAndSay2(int n) {if(n >30|| n <0) {return""; }if(n ==1) {return"1"; }Stringprevious ="1";for(int j =1; j < n; j++) {Stringstr="";charc = previous.charAt(0); int count =1;for(int i =1; i < previous.length(); i++) {charcc = previous.charAt...
public String countAndSay(int n) { String r = "1"; for (int i = 1; i < n; i++) { String t = ""; int count = 0; String flag = r.charAt(0) + ""; for (int j = 0; j < r.length(); j++) { if ((r.charAt(j) + "").equals(flag)) { count++; } else { t...
Count and Say 报数 Leetcode 38. Count and Say 报数 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/count-and-say/ 题目描述 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下: 1 被读作 "one 1" ("......
依次类推,写个countAndSay(n)函数返回字符串。 代码如下: /* * 这道题很简单,但是很难理解 * 做法就是不断的遍历求解 * */ public class Solution { public String countAndSay(int n) { String res=1+""; if(n==1) return res; else
这道计数和读法问题还是第一次遇到,看似挺复杂,其实仔细一看,算法很简单,就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。代码如下: classSolution { public:stringcountAndSay(intn) {if(n <=0)return"";stringres ="1";while(--n) {stringcur ="";for(inti =0; i < res...
LeetCode 38 Count and Say(计数与报数) 。备注:数字序列应该用字符串表示。</code> 原文 <code class="hljs applescript">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 ...
Given an integernwhere 1 ≤n≤ 30, generate thenth term of the count-and-say sequence. Note: Each term of the sequence of integers will be represented as a string. Example 1: Input: 1Output:"1" Example 2: Input: 4Output:"1211" ...
term, justcount and saythen-1 term. 总之就是第n条结果依赖于第n-1条结果。例如: 结果3=21,结果4=1211,从结果3读出1个2和1个1,所以结果4=1211。 话不多说直接上代码吧: 解题代码: classSolution{public:stringcountAndSay(intn){intcount,getnum; ...
leetcode 38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1. 2. 3. 4. 5. 1is read off as"one 1"or11. 11is read off as"two 1s"or21....