【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 ...
随机推荐
- ubuntu12.04 下安装matlab2012
1.下载matlab2012a(例如:****.iso) 2.创建挂载目录 sudo mkdir /media/matlab 3.将当前目录切换到镜像文件的目录,然后将镜像文件挂载到刚刚创建的目录下 ...
- 使用eclipse与jLink V8调试exynos 4412 u-boot
/** ****************************************************************************** * @author Maox ...
- linux下最大文件数
系统级:系统级设置对所有用户有效.可通过两种方式查看系统最大文件限制1 cat /proc/sys/fs/file-max 2 sysctl -a 查看结果中fs.file-max这项的配置数量如果需 ...
- C# app.config文件配置和修改
很多时候我们需要对系统的.config文件进度读写操作,例如:系统初始化的参数的更改.系统参数的改变都需要更新到配置文件. 首先我们有必要了解一下app.config.exe.config和vshos ...
- IMAP收邮件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- BGP学习笔记
源自红茶三杯: BGP应用于大规模网络或运营商,用作在AS间传递路由信息 使用BGP的三大理由 1. 大量路由需要承载, IGP只能容纳千条,而BGP可以容纳上万(应该是IGP结合BGP使用?) 2. ...
- unison+inotify实现文件双向自动同步
nfs适合存小图片和小文件,有一个致命的缺点,就是其中一台web服务挂掉之后,会直接导致web页面无法访问,动态的那种数据, 而且数据量很大的数据不适合nfs Unison是一款跨平台(window, ...
- WordPress 主题开发 - (四) 创建WordPress的主题HTML结构 待翻译
Now we're starting to get into the real meat of WordPress Theme development: coding the HTML structu ...
- 【FitNess】测试框架试用
参考网友的博客http://blog.csdn.net/funi16/article/details/8985280 1.官网下载jar包fitnesse-standalone.jar后安装. 2.进 ...
- php手册学习
整型:int 转换为整型:intval(str) 32位最大值64位最大值 不存在整除语法:应用round();四舍五入.integer去除小数. $a = 1234; //十进制数 $a = ...