[LeetCode 题解]: Count and Say
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之后的一个sample: 312211
如何理解呢? 将111221 拆成后面的表达式即可: 111, 22, 1 其中有 3个one , 2个two, 1个one ,读起来就是: 312211
class Solution {
public:
string countAndSay(int n) {
string a="";
if(n==) return a;
for(int i=;i<=n;i++)
{
string b;
char current = a[];
int j=,count=;
for(j=;j<a.size();j++)
{
if(a[j]==current)
{
count++;
}
else
{
char c = count +'';
b+=c;
b+=current;
current = a[j];
count=;
}
}
char c = count+'';
b+=c;
b+=current;
a= b;
}
return a;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢
[LeetCode 题解]: Count and Say的更多相关文章
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- leetcode题解#3:无重复字符的最长子串
leetcode题解:无重复字符的最长子串 题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: s = "abcabcbb"输出: 3 解释 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
随机推荐
- 【转】javascript 执行环境,变量对象,作用域链
这篇文章比较清晰的解释了一些作用域链相关的概念,忍不住收藏了 原文地址:http://segmentfault.com/a/1190000000533094 前言 这几天在看<javascrip ...
- mongodb(四)
Count+Distinct+Group数据库命令操作固定集合特性GridFS文件系统补充服务器端脚本 db.runCommand({group:{ ns:"persons", k ...
- 「小程序JAVA实战」小程序模块页面引用(18)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-18/ 上一节,讲了模板的概念,其实小程序还提供了模块的概念.源码:https://github.c ...
- Windows Nginx 基本操作
Nginx 下载 下载路径:Nginx下载 下载后解压如下图 常用命令 在nginx目录下打开命令行工具(可能需要管理员权限). start nginx : 启动nginxnginx -t 测试ngi ...
- svn 的限制
*.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store *.exe *. ...
- Camera & Render
1.void Render(); Description Render the camera manually. This will render the camera. It will use th ...
- linux下项目开发加载动态库:ldconfig与 /etc/ld.so.conf
场景:自己开发一个项目,程序里包含一些自定义动态库.运行,需要加载这些动态库. 假如这些库在/pro/output/lib/下面,可执行程序在/pro/output/bin/下面. 那么,我们需要: ...
- 这几天搞UNITY遇到的坑
都是在IPHONE设备上遇到的,UNITY版本是5.4.4f1 1.EASY AR出现扫描蓝线绿块的,是因为不是EASY AR的CameraDeviceBehavior默认参数1280X720 2.自 ...
- 用C语言进行最基本的socket编程
什么是socket 你经常听到人们谈论着 “socket”,或许你还不知道它的确切含义.现在让我告诉你:它是使用 标准Unix 文件描述符 (file descriptor) 和其它程序通讯的方式.什 ...
- 303. Range Sum Query 范围求和系列
Immutable [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ...