leetcode_38题——Count and Say(string,迭代计数)

Count and Say

Total Accepted: 39135 Total Submissions: 154215My Submissions

Question Solution

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.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

开始没理解这道题的意思,他是说求这个序列的第n个数是什么

#include<iostream>
#include<string>
#include<vector>
using namespace std;

//这道题采用迭代计数的方法,假设已知前面一个数,然后根据这个数计算出下面一个数
string the_next_number(string vec)
{
	string temp_last;
	string temp;
	temp.push_back(vec[0]);
	if(vec.size()>1)
	{
		int i=1;
		while(i!=vec.size())
		{
			if(vec[i]==vec[i-1])
			{
				temp.push_back(vec[i]);
				i++;
			}
			else
			{
				temp_last.push_back(temp.size()+48);
				temp_last.push_back(vec[i-1]);
				temp.clear();
				temp.push_back(vec[i]);
				i++;
			}
		}
	}
	if(temp.size()!=0)
	{
		temp_last.push_back(temp.size()+48);
		temp_last.push_back(temp[0]);
	}
	return temp_last;
}

string countAndSay(int n) {
	string str_result="1";
	if(n==1)
		return str_result;
	else
		n=n-1;
	while(n--)
	{
		str_result=the_next_number(str_result);
	}
	return str_result;
}

int main()
{
	cout<<countAndSay(1)<<endl;
	cout<<countAndSay(2)<<endl;
	cout<<countAndSay(3)<<endl;
	cout<<countAndSay(4)<<endl;

	system("pause");
	return 1;
}

  

时间: 2024-08-04 17:02:24

leetcode_38题——Count and Say(string,迭代计数)的相关文章

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

LeetCode 第 204 题 (Count Primes)

LeetCode 第 204 题 (Count Primes) Description: Count the number of prime numbers less than a non-negative number, n. 计算小于 N 的素数的个数.这道题目比较简单.但是想提高计算效率与需要费点脑筋. 判断一个数字 n 是不是素数的简单方法是 用 n 去除 2,3,4,-,n?1,如果都不能整除就说明这个数是素数. 按照这个思路可以写个简单的函数. bool isPrime(int n)

【leetcode刷题笔记】Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false. 题解:DP问题. 用数组dp[i][

【leetcode刷题笔记】Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

【算法】LeetCode算法题-Count And Say

这是悦乐书的第153次更新,第155篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第12题(顺位题号是38).count-and-say序列是整数序列,前五个术语如下: 1 11 21 1211 111221 1被读作"一个一"或者11.第二项的值是第一项的读法. 11被读作"两个一"或者21.第三项的值是第二项的读法. 21被读作"一个二,两个一"或者1211.第四项的值是第三项的读法. 给定整数n,其中1≤n≤3

LeetCode算法题-Count Binary Substrings(Java实现)

这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串的数量,并且这些子串中的所有0和所有1都是连续的.重复出现的子串也计算在内.例如: 输入:"00110011" 输出:6 说明:有6个子串具有相同数量的连续1和0:"0011","01","1100","10"

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

LeetCode题828 —— Unique Letter String

https://leetcode.com/problems/unique-letter-string/description/ A character is unique in string S if it occurs exactly once in it. For example, in string S = "LETTER", the only unique characters are "L" and "R". Let's define 

leetcode_125题——Valid Palindrome(string,比较常规思路)

Valid Palindrome Total Accepted: 48909 Total Submissions: 221328My Submissions Question Solution Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama