classSolution{public:stringcountAndSay(intn){intcount,getnum; string res="1";if(n==1)returnres; res="11";for(inti=2;i<n;i++){ string save=res; res=""; getnum=save[0]; count=1;for(intj=1;j<save.length();j++){if(save[j]==save[j-1])count++;else{ res+=char(count+48...
}stringcountAndSay(intn){stringres ="1";while(--n) { res = read(res); }returnres; } 解法2:调整了解法1中read函数的处理逻辑。同样从左往右遍历一遍,若与后一个数相同,计数值count加1;若不同,将计数值和当前位置数值插入字符串,同时重置计数值。为了防止最后一位比较时出现错误,在原字符串末尾添加...
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211. Given an integern, generate thenthsequence. Note: The sequence of intege...
Leetcode 38. Count and Say 报数 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/count-and-say/ 题目描述 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下: 1 被读作 "one 1" ("...LeetCode 38. Count and Say 解法一: 解法二: ...猜...
原题leetcode地址:https://leetcode.com/problems/count-and-say/ 1. 定义变量count,如果相邻的来给你个数相等,那么count++。否则将count和这个数存放,注意最后一个数 时间复杂度:O(n^2),嵌套for循环。 空间复杂度:O(1),没有申请额外的空间。
[LeetCode] Count and Say 计数和读法 #include #include using namespace std; string CountAndSay(int n) { string res = “”; int num = -1; int sum = 0; char chsum; char chremind; while (n) { int remind = n % 10; int quotient = n / 10; if (num == -1) { num = ...
5.Leetcode 38:Count and Say 笔记 1:题目描述 Given an integern, generate thenthterm of the count-and-say sequence. 找规律,给定一个数n后,写出第n行的字符串 2:题目分析 突然发现自己的Say实在不发达,看了半个多小时才知道咋回事。n就是再说n-1行的内容:“有‘x1’个‘num1’和‘x2’个‘num...
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....
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)) { ...
1.用2个串来替换存储记录result,tmp。 2.记录当前值n,获取当前值最大连续长度m。 3.tmp串追加"m",再追加"n"。 代码如下: classSolution{public:stringcountAndSay(intn){if(--n <0)return"";stringresult ="1";stringtmp;//临时串intstep =1;//步长charcur;//当前元素while(n) ...