LeetCode(38)题解: Count 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.

思路:

题意有些模糊,看明白1211到111221就懂了。

AC代码:

 1 class Solution {
 2 public:
 3     string countAndSay(int n) {
 4         int tmp_n,count,k;
 5         string tmp,tmp_res,a[n];
 6         a[0]="1";
 7         for(int i=1;i<n;i++){
 8             tmp=a[i-1];
 9             tmp_res="";
10             tmp_n=tmp.size();
11             k=0;
12             count=1;
13             for(int j=1;j<tmp_n;j++){
14                 if(tmp[j]==tmp[j-1]){
15                     count++;
16                     continue;
17                 }
18                 else{
19                     tmp_res.push_back(‘0‘+count);
20                     tmp_res.push_back(tmp[j-1]);
21                     count=1;
22                 }
23             }
24             tmp_res.push_back(‘0‘+count);
25             tmp_res.push_back(tmp[tmp_n-1]);
26             a[i]=tmp_res;
27         }
28     return a[n-1];
29     }
30 };
时间: 2024-10-27 10:06:48

LeetCode(38)题解: Count and Say的相关文章

LeetCode: LetterCombinations 题解

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode: palindromes 题解

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

LeetCode: plusOne 题解

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意: 给定一个由一组数字构成的数组表示的非负整数, 对这个数进行加一操作,并将结果数组返回. 数组的首元素存储的是该非负整数的最高位. 本题比较简

leetCode 38. 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" 

LeetCode 38 Count and Say(字符串规律输出)

题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111221—>312211—>…. 按照上面的规律进行求解出第n个字符串是什么. 规律:相连的数字有多少个然后添加上这个数字 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 按照规律进行求解字符串 */ public class So

Java [leetcode 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" or 1211.

leetCode 38.Count and Say (计数和发言) 解题思路和方法

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" o

[LeetCode 38] Count and Say

题目链接: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 on

LeetCode(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" or 1211. Given