【Count and Say】cpp
题目:
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.
代码:
class Solution {
public:
string countAndSay(int n) {
string tmp1 = "";
string tmp2 = "";
for ( size_t i = ; i < n; ++i )
{
int digit_count = ;
for ( size_t j = ; j < tmp1.size(); ++j )
{
if ( tmp1[j]==tmp1[j-] )
{
++digit_count;
}
else
{
tmp2 += digit_count+'';
tmp2 += tmp1[j-];
digit_count = ;
}
}
tmp2 += digit_count+'';
tmp2 += tmp1[tmp1.size()-];
tmp1 = tmp2;
tmp2 = "";
}
return tmp1;
}
};
tips:
这个题意不太好理解。
简单说就是:第n组字符串是第n-1组字符串的读法;读法的准则就是‘连续出现个数+数字’。
其余的就是处理一下边界case。
=======================================
第二次过这道题,对题意理解好了之后,代码一次AC。
class Solution {
public:
string countAndSay(int n) {
if (n<) return "";
string ret = "";
for ( int i=; i<n; ++i )
{
string tmp_ret = "";
int count_same_digit = ;
char curr_digit = ret[];
for ( int j=; j<ret.size(); ++j )
{
if ( ret[j]!=ret[j-] )
{
tmp_ret += string(,count_same_digit+'') + string(,curr_digit);
curr_digit = ret[j];
count_same_digit = ;
}
else
{
count_same_digit++;
}
}
tmp_ret += string(,count_same_digit+'') + string(,curr_digit);
ret = tmp_ret;
}
return ret;
}
};
【Count and Say】cpp的更多相关文章
- 【Integer To Roman】cpp
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- hdu 3336【Count the string】(KMP)
一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
随机推荐
- VPN连接失败
连接VPN是总提示 本来我以为是VPN服务器的问题,可是别人就能连接成功,所以只能是我自己机子的问题.后来检查了一下连接属性,终于发现了问题: 这里“允许使用这些协议”应该是处于选中状态,而我的属性里 ...
- Google账户无法登陆-Solved
Author:KillerLegend Date:2014.5.19 From:http://www.cnblogs.com/killerlegend/p/3737888.html 这几天不知道怎么回 ...
- Oracle 手动收集统计信息
收集oracle统计信息 优化器统计范围: 表统计: --行数,块数,行平均长度:all_tables:NUM_ROWS,BLOCKS,AVG_ROW_LEN: 列统计: --列中唯一值的数量(NDV ...
- 第一节:CLR寄宿
本系列文章来自 CLR VIA C# .NET FrameWork在Microsoft Windows平台的顶部运行.这意味着.NET必须用Windows可以理解的技术来构建.首先,所有的托管模块和 ...
- Python科学计算利器——Anaconda
(搬运自我在SegmentFault的博客) 最近在用Python做中文自然语言处理.使用的IDE是PyCharm.PyCharm确实是Python开发之首选,但用于科学计算方面,还略有欠缺.为此我尝 ...
- Java 逻辑运算符、位运算符、移位操作符 总结(Java 学习中的小记录)
Java 逻辑运算符.位运算符.移位操作符 总结 作者:王可利(Star·星星) 逻辑运算符,表格如下: 解析逻辑运算符表: 1.与 (&) 特点:两个都为真的时候,结果为真.两个为 ...
- 学习KMP算法
int kmp(char * t,int lenT,char * pat,int lenPat){ ,posT=; int[] f=partialMatch(pat,lenPat)//获取pat字符串 ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- golang与C交互:cgo
1. 在Go中引用C代码很简单, 在 import "C"前用注释引入标准的C代码, 然后使用C.xxx的伪包引用C代码空间的标识符即可. 需要注意, import"C& ...
- iOS App Launch Option
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...