报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1 被读作 "one 1" ("一个一") , 即 11。
11 被读作 "two 1s" ("两个一"), 即 21。
21 被读作 "one 2", "one 1" ("一个二" , "一个一") , 即 1211。
给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。
注意:整数顺序将表示为一个字符串。
示例 1:
输入: 1
输出: "1"
示例 2:
输入: 4
输出: "1211"

#include <iostream>
using namespace std; string countAndSay(int n) {
string ans="";
int count;
char temp1;
string temp2;
for(int j=;j<n-;j++)//n的值控制循环了几次
{
temp2=ans;
ans="";//每次都要把ans重新置空
int t=temp2.length();
for(int i=;i<t;i++)
{
count=;
temp1=temp2[i];
while(i+<temp2.length()&&temp2[i]==temp2[i+])//记录后面有多少相同的
{
count++;
i++;
}
ans+=to_string(count)+temp1;//更新ans
}
}
return ans;
} int main() {
std::cout << countAndSay()<<endl << countAndSay()<<endl<< countAndSay()<<endl<< countAndSay()<<endl<< countAndSay()<<endl<< std::endl;
return ;
}

#leetcode刷题之路38-报数的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

  9. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

随机推荐

  1. WPF ListView ListBox 常用的样式记录

    ListView: <ListView x:Name="lvBlockedApps" ItemsSource="{Binding BlockedAppsCollec ...

  2. Difference between 'SAME' and 'VALID' padding

    Difference between 'SAME' and 'VALID' padding 'SAME' padding 和 'VALID' padding 的区别 If you like ascii ...

  3. 如何调试flutter应用

    The Dart Analyzer 这个工具帮助你分析代码,发现可能的错误. 运行命令行 终端进入flutter工程所在目录,执行flutter analyze 使用IntelliJ IDEA Dar ...

  4. 线程间的通信方式2--管道流Pipes

    “管道”是java.io包的一部分.它是Java的特性,而不是Android特有的.一条“管道”为两个线程建立一个单向的通道.生产者负责写数据,消费者负责读取数据. 下面是一个使用管道流进行通信的例子 ...

  5. 关于removeChild

    var a=document.body; document.body.parentNode.removeChild(a) console.log(a); a的内容还是body???whY

  6. 如何在定制化组件中实现并使用v-model

    https://alligator.io/vuejs/add-v-model-support/

  7. 让两个对象间建立weak关系

    让两个对象间建立weak关系 这是为了给两个对象间建立weak关系,当一个对象被释放时,另外一个对象再获取这个值时就是nil,也就是不持有这个对象:) 源码: WeakRelatedDictionar ...

  8. iOS手势处理

    iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...

  9. UNIX日期与时间

    日期和时间 UINX系统内部有一个变量记录自开机以来经过的时间.从用户的角度,UNIX时间函数分为3类: 度量进程已使用CPU时间的函数: 给出绝对时间或日历时间的函数: 设置闹钟.定时器以及睡眠的函 ...

  10. maven项目中引入Jstl

    <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api& ...