【Leetcode】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.

-----------------------------------------------------------------------------------------------

以上含义描述不清,题目的实际意思是:

n = 0:  1

n = 1:  11  (前一个是1个1)

n = 2:  21  (前一个是2个1)

n = 3:  1211 (前一个是1个2,1个1)

n = 4:  111221  (前一个是1个1,1个2,2个1)

....

所以是一个递推关系,按规则模拟即可。

 1 class Solution {
2 public:
3 string countAndSay(int n) {
4 string result("1");
5 for (int i = 1; i < n; ++i) {
6 stringstream ss;
7 int j = 0, N = result.size();
8 while (j < N) {
9 int c = 1;
10 while (j + 1 < N && result[j] == result[j + 1]) {
11 ++c;
12 ++j;
13 }
14 ss << c << result[j];
15 ++j;
16 }
17 ss >> result;
18 }
19 return result;
20 }
21 };

时间: 2024-10-13 09:18:38

【Leetcode】Count and Say的相关文章

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/ 题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range

【leetcode】Count and Say (easy)

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

【leetcode】Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. 1 class Solution { 2 public: 3 int countPrimes(int n) { 4 vector<bool> num(n-1,true); 5 num[0]=false; 6 int res=0; 7 int limit=sqrt(n); 8 9 for(int i=2;i<=limit;i++

【leetcode】Count Primes(easy)

Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字是否可以整除已有质数.然后妥妥的超时了(⊙v⊙). 看提示,发现有个Eratosthenes算法找质数的,说白了就是给所有的数字一个标记,把质数的整数倍标为false,那么下一个没被标为false的数字就是下一个质数. int countPrimes(int n) { if(n <= 2) retu

【Leetcode】Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the righ

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode】Candy

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov