博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]38.Count and Say
阅读量:7014 次
发布时间:2019-06-28

本文共 1445 字,大约阅读时间需要 4 分钟。

【题目】

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.

【分析】

111221 统计相邻重复的字符个数,例如这里111有3个1重复相邻,输出31,22有两个2重复相邻,输出22,1只有一个1,输出11

【代码】

/**********************************   日期:2015-01-27*   作者:SJF0115*   题目: 38.Count and Say*   网址:https://oj.leetcode.com/problems/count-and-say/*   结果:AC*   来源:LeetCode*   博客:**********************************/#include 
using namespace std;class Solution {public: string countAndSay(int n) { if(n <= 0){ return ""; }//if string str("1"); for(int i = 1;i < n;++i){ NextCountAndSay(str); }//for return str; }private: void NextCountAndSay(string& str){ int len = str.length(); string tmp = ""; for(int i = 0;i < len;++i){ int repeatCount = 1; // repeat char count while((i+1 < len) && (str[i] == str[i+1])){ ++repeatCount; ++i; }// tmp += to_string(repeatCount); tmp += str[i]; }//for str = tmp; }};int main(){ Solution solution; int n = 5; string result = solution.countAndSay(n); // 输出 cout<
<

转载地址:http://omhtl.baihongyu.com/

你可能感兴趣的文章
《Android开发从零开始》——7.Intent初级学习
查看>>
Cacti weathermap添加实时读数节点
查看>>
Linux就该这么学
查看>>
Qmail 邮件系统维护管理技术文档
查看>>
Google Guava - Cache
查看>>
你以为的SPSS只是简单的数据分析软件吗?
查看>>
CSS Grid Simple Example
查看>>
《Windows服务器配置与管理》本地用户、组的管理
查看>>
mysql数据库移植sqlserver数据库
查看>>
使用update-alternatives 命令修改Java版本-自动切换Java版本
查看>>
我的友情链接
查看>>
linux命令学习(68 69)-tree finger
查看>>
写给同事的前端学习路线
查看>>
我的友情链接
查看>>
Global Azure上创建SSTP模式***配置介绍
查看>>
SQL Server 2016 Failover Cluster+ ALwaysOn(二)
查看>>
spring源码--依赖注入
查看>>
rhel7配置多用户tiger vnc server
查看>>
iOS播放系统声音
查看>>
const 命令
查看>>