【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列
(一)题目
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.
(二)解题
/*
首先初始化字符串为1,对应一个1,读成11
字符串更新为11,对应两个1,读成21
字符串更新为21,对应1个2一个1,读成1211
....
依次进行n次则得到最后的字符串
*/
class Solution {
public:
string countAndSay(int n) {
string str = "1";
while(--n)
{
string ret;
for (int i = 0; i < str.length(); ) {
int count = 0;
while (i < str.length() && str[i] == str[i + 1]) {
count++;
i++;
}
ret += ('1' + count);
ret += str[i];
i++;
}
str = ret;
}
return str;
}
};
【一天一道LeetCode】#38. Count and Say的更多相关文章
- 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(36ms)
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
随机推荐
- 20160225.CCPP体系详解(0035天)
程序片段(01):CircleList.h+CircleList.c+main.c 内容概要:环形链表 ///CircleList.h #pragma once #include <stdio. ...
- Android艺术开发探索——第二章:IPC机制(下)
Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...
- pdflush的工作原理
大家知道,在linux操作系统中,写操作是异步的,即写操作返回的时候数据并没有真正写到磁盘上,而是先写到了系统cache里,随后由pdflush内核线程将系统中的脏页写到磁盘上,在下面几种情况下,系统 ...
- Android简易实战教程--第三十话《撕衣美女》
此篇邪恶一些,给单身屌丝发点"福利",通过图片的绘制,给美女脱掉衣服. 原理:图片覆盖图片,通过画笔对顶端的图片做一些特效处理,即手指触摸的地方,设置为透明.即可显示最底部的美女图 ...
- Android Studio中Git的配置及协同开发
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51595096 本文出自:[openXu的博客] 目录: 一 Android Stutio配置 ...
- FFmpeg的HEVC解码器源代码简单分析:环路滤波(Loop Filter)
===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...
- Erlang的常驻模块与功能模块
Erlang的常驻模块与功能模块Residence moduleThe module where a process has its tail-recursive loop function(s).I ...
- 【移动开发】EditText输入字数限制总结(包括中文输入内存溢出的解决方法)
限定EditText输入个数的解决方案很多,但是一般主要考虑两点,也就是处理两件事:(1)不同语言字符(英文.中文等)处理方式(2)输入字符达到数目后,是否仍然允许用户输入 第一点,涉及的东东其实蛮多 ...
- 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理
Linux下的qt安装,命令时:sudoapt-get install qt-sdk 安装mysql数据库,安装方法参考博客:http://blog.csdn.net/tototuzuoquan ...
- ROS讲座 关于ROS2和Gazebo C++ in Open Source Robotics
分享一个不错的介绍ROS2和Gazebo的视频讲座. Gazebo中的云彩飘动起来了!!!! 超清视频分享网址: http://v.youku.com/v_show/id_XMTcyMzY0Nz ...