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=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推(不断数数),写个countAndSay(n)函数返回字符串。
class Solution {
public:
string convert(const string &say)
{
stringstream ss; //创建一个流
int count=0;
char last=say[0];
for(size_t i=0; i<=say.size(); ++i)
{
if(last==say[i]) //每次与前一个字符比较是否相等,相等则计数加一
{
++count;
}
else
{
ss<<count<<last; //将count的值传递到流ss中,再将last的值传入
count=1;
last=say[i];
}
}
return ss.str(); //返回流中的字符串
}
string countAndSay(int n) {
if(n<=0) return string();
string say="1"; //定义初始字符串的内容
for (int i=1; i<n; ++i)
{
say=convert(say);
}
return say;
}
};
或:
class Solution {
public:
string nextRead(string s) {
stringstream ss;
int count, i = 0, n = s.length();
while (i < n) {
count = 0;
while (i + 1 < n && s[i] == s[i + 1]) {
i++;
count++;
}
ss << count + 1 << s[i];
i++;
}
return ss.str();
}
string countAndSay(int n) {
string res = "";
if (n == 0) return res;
res = "1";
if (n == 1) return "1";
while (n > 1) {
res = nextRead(res);
n--;
}
return res;
}
};
其他解法:
class Solution {
public:
string countAndSay(int n) {
if (n==0){
return NULL;
}
if (n==1){
return "1";
}
int count=1;
string s1="1";
string s2;
int j=1;
if (n>=2){
s1="11";
j=2;
}
int i=0;
while (j<n && n>1){
s2="";
for (i=0;i<s1.size();i++){
for (int m=i;m<s1.size()-1;m++){
if (s1[m]==s1[m+1]){
count++;
i++;
}
if (s1[m]!=s1[m+1]){
break;
}
}
s2+=to_string(count)+s1[i];
count=1;
}
s1=s2;
j++;
}
return s1;
}
};
class Solution {
public:
string countAndSay(int n) {
if(n==0) return " ";
if(n==1) return "1";
string str="1";
string tmp;
for(int i=1;i<n;++i)
{
tmp.clear();
int cnt=1;
for(int j=0;j<str.size();++j)
{
while(j+1<str.size())
{
if(str[j]==str[j+1])
{
++cnt;
++j;
}
else
break;
}
tmp.push_back(cnt+'0');
tmp.push_back(str[j]);
cnt=1;
}
str=tmp;
}
return str;
}
};
leetcode:Count and Say的更多相关文章
- leetcode:Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- LeetCode:数据库技术【175-178】
LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...
- LeetCode:螺旋矩阵||【59】
LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...
- LeetCode:旋转链表【61】
LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...
- LeetCode:乘法表中的第K小的数【668】
LeetCode:乘法表中的第K小的数[668] 题目描述 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要 ...
随机推荐
- idea maven添加jar包
在“项目结构“里设置 选择libaray,添加jar包
- 原生js实现中文时钟
零.寒暄 终于一个月可以更新两篇博客了,开心.昨天花了大概一天的时间玩了下github,基本的clone和push都搞定了,如果有和我一样的新手没调通的,大家可以交流. 另外,说个题外话,大家发现我的 ...
- VSFTPD全攻略(/etc/vsftpd/vsftpd.conf文件详解)
/etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 #################匿名权限控制############### anonymous_enable=YE ...
- html5碰撞小球模拟
这里根据动量守恒和能量守恒定理来计算小球的位置,从而模拟完全弹性碰撞下的小球运行轨迹. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- CNAME
CNAME指别名记录也被称为规范名字.这种记录允许您将多个名字映射到同一台计算机. 通常用于同时提供WWW和MAIL服务的计算机.例如,有一台计算机名为“host.mydomain.com”(A记录) ...
- SSH无密码验证
一.安装和启动SSH协议 sudo yum install ssh sudo yum install rsync service sshd restart 启动服务 (rsync是一个远程数据同步工具 ...
- SGU 102
For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprim ...
- Codeforces Round #337 (Div. 2) B. Vika and Squares 水题
B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from ...
- poj 3255(次短路)
题目链接:http://poj.org/bbs?problem_id=3255 思路:分别以源点1和终点N为源点,两次SPFA求得dist1[i](1到各点的最短距离)以及dist2[i](各点到N的 ...
- linux下ssh/scp无密钥登陆方法
一.双方机器都是root用户登陆方法 A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux 在A ...