LeetCode 38. Count and Say
来源:互联网 发布:sql中建立表的命令是 编辑:程序博客网 时间:2024/05/22 17:06
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.
I dont actually agree this is an easy one.... I made mistakes anyway....
#include <string>#include <vector>#include <iostream>using namespace std;string nextString(string tmp) { string nextString = ""; for(int i = 0; i < tmp.size(); ++i) { int count = 1; while(i + 1 < tmp.size() && tmp[i] == tmp[i+1]) { i++; count++; } nextString += to_string(count) + tmp[i]; } return nextString;}string countAndSay(int n) { string tmp = "1"; for(int i = 1; i < n; ++i) { tmp = nextString(tmp); } return tmp;}int main(void) { cout << countAndSay(3) << endl;}// Second Versionstring nextStringII(string tmp) { string res = ""; int count = 1; for(int i = 1; i < tmp.size(); ++i) { if(tmp[i] == tmp[i-1]) { count++; } else { res += to_string(count) + tmp[i-1]; count = 1; } } int n = tmp.size(); if(count > 1) return res += to_string(count) + tmp[n - 1]; return res += to_string(count) + tmp[n-1];}
0 0
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- Leetcode 38. Count and Say
- leetcode 38. Count and Say
- [leetcode] 38. Count and Say
- LeetCode-38.Count and Say
- LeetCode *** 38. Count and Say
- leetcode 38. Count and Say
- LeetCode-38. Count and Say
- LeetCode 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode - 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode 38. Count and Say
- LeetCode 38. Count and Say
- 数据结构与算法01--堆栈 & 队列
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- Struts2框架学习(一)
- 【bzoj2666】【cqoi2012】【组装】【贪心】
- c++实验4 项目一 1000以内所有偶数之和
- LeetCode 38. Count and Say
- 搭建hadoop的高可用性(HA)集群
- Android日期时间选择对话框
- Struts2框架学习(二) Action
- openstack中的消息总线
- 寻找一个单向链表的中项,如果存在两个则返回前一个,给出算法描述
- input type="radio" 的用法
- js 设置header,实现跨域访问
- 第8周项目1 数组做数据成员 工资类(2)