LeetCode 038 Count and Say
题目要求: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.
分析:
看了半天才看懂这道题……
代码如下:
class Solution {
public:
string countAndSay(int n) {
string s("1");
while(--n)
s = getNext(s);
return s;
}
string getNext(const string &s){
stringstream ss;
for(auto i = s.begin(); i != s.end(); ){
auto j = find_if(i, s.end(), bind1st(not_equal_to<char>(), *i));
//几个i,核心
ss << distance(i, j) << *i;
i = j;
}
return ss.str();
}
};
LeetCode 038 Count and Say的更多相关文章
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- Java for LeetCode 038 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
随机推荐
- linux修改进程名
一.linux中的进程名 linux中有很多查看/操作进程的命令. 这些命令的参数或显示的结果,有的是真实的进程名(top/pstree/pgrep/kill/killall),有的是进程 ...
- JavaWeb中的关于html、jsp、servlet下的路径问题
1 前言 本文将对近期项目练习中出现的关于文件路径的问题进行分析和总结,主要涉及html页面中的href及ajax指向路径.jsp页面中href指向路径及servlet转发或重定向路径等内容,本文的分 ...
- 重载符operator() -- effective c++ 3rd P71的的隐式类型转换及相关的研究
class的"operator 返回类型 ()" 的重载 就是对(class)的重载,这个重载符不用参数,参数就是自身,并且与函数传递的参数括号等价 如 func(c), 并且多个 ...
- Dcoker 安装 rabbitMq
使用Docker安装部署RabbitMQ 1.docker search rabbitmq:management 2.docker pull rabbitmq:management ...
- 思科ASA放行主/被动FTP
实验环境: 设备说明: internet是一台windows10,用于模拟外网客户 ASA是思科ASA防火墙 FTP-SERVER是Centos7,Centos7上安装了vsftpd 实验说明: 本文 ...
- ClassNotFoundException: java.util.ArrayList$SubList 错误
ClassNotFoundException: java.util.ArrayList$SubListjava.lang.RuntimeException: java.lang.ClassNotFou ...
- Selective Acknowledgment 选项 浅析 1
抓包的时候,发现 tcp 三次握手中一般会有几个options 一个是mss 一个是ws 一个sack perm 这次主要是来说一说 sack 这个选项: 1. 只重传超时的数据包,比较实用与后 ...
- Boost命令行解释器的简单使用:Boost.Program_options
简介 如果使用比较多的命令行程序的话,对于命令行参数的输入肯定不会陌生,大部分的程序都是通过类似下面的形式进行输入的,比如熟悉的ls ls --all -l --color=auto 这里面包含了三种 ...
- JPA、Hibernate、Spring-Data-Jpa的本质区别
什么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1)ORM映射元数据:JPA支持 ...
- beef+metasploit
beef调用metasploit模块,直接xss吊打 先进入beef的文件夹 对config.yaml进行修改 将metasploit的false改为true 进入这个文件夹 修改配置文件 检查met ...