【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 ...
随机推荐
- 较好的IOS新闻客户端应用源码
兼容性较好的新闻客户端应用源码,这个是一款国外新闻客户端源码,并且这款应用兼容性非常好的,可以很好地兼容iPhone和iPad的使用,而且应用的功能很多,新闻列表,上啦下拉刷新效果,评论列表,在线评论 ...
- [原]sdut2624 Contest Print Server (大水+大坑)山东省第四届ACM省赛
本文出自:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&am ...
- iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统
这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...
- [原]打造Python开发环境之初篇
古语有云: 工欲善其事,必先利其器 拥有自己的一套得心应手的Python开发环境,开发起来,简直如丝般顺滑.以我工作中使用到的Python开发环境(主要是Web应用),先做个总体介绍 Python环境 ...
- linux中文显示乱码的解决办法
linux中文显示乱码的解决办法 linux中文显示乱码是一件让人很头疼的事情. linux中文显示乱码的解决办法:[root@kk]#vi /etc/sysconfig/i18n将文件中的内容修改为 ...
- [原创]pg_shard使用场景及功能测试
pg_shard是一个PostgreSQL的sharding extension.可以用于Shards.Replicates tables和高可用.它可以在不修改Applications的情况下无缝分 ...
- Oracle出现字符集问题处理方法
1. Cmd进去DOS 2. 再输入dbca(database create) 3. 弹出的界面,直接下一步,选择删除数据库 4. 成功删除后,回到一第一界面,选择创建数据库,下一步. 5. ...
- React Native分析(index.ios.js)
定义创建组件MyComponent(index.ios.js): 'use strict' var React = require('react-native'); var { AppRegistry ...
- C#.Net 图片处理大全
C# How to: Image filtering by directly manipulating Pixel ARGB values C# How to: Image filtering imp ...
- c++基础(三):多态
virtual:虚函数.C++通过虚函数实现多态."无论发送消息的对象属于什么类,它们均发送具有同一形式的消息,对消息的处理方式可能随接手消息的对象而变"的处理方式被称为多态性.& ...