【leetcode】Count and Say (easy)
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.
十天没做题了,赶快秒个小题练练手。
思路:就是数每种数字出现了几次。
- string countAndSay(int n) {
- string s = "";
- while(--n) //从1开始计数
- {
- string stmp;
- char c[];
- for(int i = ; i < s.length(); )
- {
- int count = ;
- char cur = s[i];
- while(i < s.length() && s[i] == cur) //数当前重复出现的数字
- {
- count++;
- i++;
- }
- sprintf(c, "%d%d", count, cur - '');
- stmp += c;
- }
- s = stmp;
- }
- return s;
- }
【leetcode】Count and Say (easy)的更多相关文章
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【LeetCode】Agorithms 题集(一)
Single Number 题目 Given an array of integers, every element appears twice except for one. Find that s ...
- 【LeetCode】Algorithms 题集(三)
Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...
随机推荐
- 国内SEO如何过滤掉不良网络信息
对于站长们来说,首要任务就是和搜索引擎战斗,面对搜索引擎算法的不断更新,站长们也更加头疼.站长们都觉得,搜索引擎才是网站优化的"统治者",和谷歌优化相比,中国的SEO优化要复杂的多 ...
- html 补充
替换文本属性(Alt)alt 属性用来为图像定义一串预备的可替换的文本.替换文本属性的值是用户定义的.<img src="boat.gif" alt="Big Bo ...
- 微信事业群WXG成立 致力于打造微信大平台
今天,微信之父张小龙带领微信团队成立微信事业群(WeiXin Group,简称WXG),致力于打造微信大平台,由他们负责微信基础平台.微信开放平台.微信支付拓展.O2O等微信延伸业务的发展,并包括邮箱 ...
- js搜索框输入提示(高效-ys8)
<style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...
- Knockout.Js案例二Working With Lists And Collections
案例一:Foreach绑定 通常,您要生成重复的UI元素,特别是当显示列表,用户可以添加和删除元素.KO.JS让你轻松,使用的数组和foreach绑定. 在接下来的几分钟,您将构建一个动态UI保留席位 ...
- 深入理解Java虚拟机之读书笔记二 垃圾收集器
1.对象已死? a.引用计数算法:缺点是它很难解决对象之间的相互循环引用的问题,Java语言中没有选用它. b.根搜索算法(GC Roots Tracing):通过一系列的名为"GC Roo ...
- 4.9---二叉树路径和(CC150)
//注意,1,要判断null:2,要注意ArrayList直接复制会被一起改变.要通过new的方式来操作.public class Solution { public static void main ...
- 3.2---最小栈(CC150)
//思路:入栈时不是最小值,永远都没机会成为最小值. import java.util.Stack; class MinStack { private Stack<Integer> sta ...
- ffmpeg解码音频流
//初始化.注册编解码器 avcodec_init(); av_register_all(); avformat_network_init(); //选取测试文件 char* FileName=&qu ...
- Scala教程
Scala表示可扩展性语言,是一种混合函数式编程语言.它是由Martin Odersky创建,并于2003年首次发布. Scala平滑地集成面向对象和函数式语言的特点,并且Scala被编译在Java虚 ...