LeetCode - 38. Count and Say(36ms)
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Example 2:
Input: 4
Output: "1211"
class Solution {
public:
string countS(string s) {
int l = s.length();
char curChar = s[];
string retStr = "";
int curN = ;
for (int i = ; i < l; i++) {
if (curChar == s[i]) {
curN += ;
if (i == l - ) {
char temp = curN + '';
retStr = retStr + temp + curChar;
}
}
else {
char temp = curN + '';
retStr = retStr + temp + curChar;
curChar = s[i];
curN = ;
if (i == l - ) {
retStr = retStr + '' + curChar;
}
}
}
return retStr;
} string countAndSay(int n) {
if (n == ) {
return "";
}
else {
string s = "";
for (int i = ; i <= n; i++) {
s = countS(s);
}
return s;
}
}
};
LeetCode - 38. Count and Say(36ms)的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
随机推荐
- 【办公-Word-VB】人民币大写转换-带完整注释
完整代码见:我的CSDN博客 -------------------- 应公司财务人员的请求,需在Word中做个:输入阿拉伯数字,自动转换成大写,并填充到Word控件中对应的亿.万.千控件格子的功能, ...
- PHP中级面试经历
资源来自网络:http://www.2cto.com/kf/201304/204749.html 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) 2.echo(),p ...
- data-ng-click 指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- ipython notebook开通远程
之前只是会用,别人告诉我命令和大概怎么设置的,今天自己搭建才发现一知半解搞不定啊. 目的:远程通过ipython notebook调用服务器. 服务器是ubuntu16.04 本地机器win7 配置方 ...
- Element.getBoundingClientRect()
Element.getBoundingClientRect()方法会返回元素的大小和相对于视口的位置 语法: var domRect = element.getBoundingClientRect() ...
- Redis集群的安装和使用
- 模块socket使用
什么是socket:socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.我们无需再去深入理解tcp/udp协议,按照socket的规定去使用就行了. 首先一个c/s架构:分为两 ...
- ETO的公开赛T1《矿脉开采》题解(正解)(by Zenurik)
作为T1,当然是越水越好啦qwq 显然经目测可得,那个所谓的质量评级根本就没卵用,可以直接\(W_i = W_i^{V_i}\)累积到利润里面. 这样,本问题显然是一个"子集和"问 ...
- 【TOJ 3692】紧急援救
#include<iostream> #include<algorithm> #include<queue> using namespace std; #defin ...
- Ganglia监控安装配置
172.17.20.123 node1 gmetad.gmond.web 172.17.20.124 node2 gmond 1.服务器安装好epel源后,安装ganglia yum install ...