leetcode 38

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 an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

说真的刚开始真不知道这道题要干什么,英语有点差~~~

当明白是什么意思的时候又发现描述起来有点问题,语文有点差~~~

生硬的描述一下吧:1被读作“1个1” 或  11;11被读作“2个1”或21;21被读作“1个2,1个1”或1211;依次这样下去,输出第n个序列。

代码如下:

 1 class Solution {
 2 public:
 3     string countAndSay(int n) {
 4         string first = "1";
 5         for(int i = 1; i < n; i++)
 6         {
 7            string ss = "";
 8            int count = 1;
 9            for(int j = 1; j < first.length(); j++)
10            {
11                if(first[j-1] == first[j])
12                {
13                    count ++;
14                }
15                else
16                {
17                    ss = ss + (char)(count + ‘0‘) + first[j-1];
18                    count = 1;
19                }
20            }
21            first = ss + (char)(count + ‘0‘) + first[first.length()-1];
22         }
23         return first;
24     }
25 };
时间: 2024-07-30 02:22:33

leetcode 38的相关文章

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/ 题目: 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

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 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: 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 121

leetCode(38):Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[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