Count and Say
Count and Say
https://leetcode.com/problems/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.
算法思想:
- 可以根据当前的这个string,来计算下一个string,如“1211”,得到“one 1 one 2 two 1”,即"111221"
程序代码:
public class Solution {
public String next(String s) {
int length = s.length();
char pre = s.charAt(0);
int count = 1;
StringBuilder sb = new StringBuilder();
for (int i = 1; i < length; i++) {
if (s.charAt(i) == pre) {
count++;
} else {
sb.append(count);
sb.append(pre);
count = 1;
pre = s.charAt(i);
}
}
sb.append(count);
sb.append(pre);
return sb.toString();
}
public String countAndSay(int n) {
if (n <= 0) {
return "";
}
String cas = "1";
for (int i = 2; i <= n; i++) {
cas = next(cas);
}
return cas;
}
}
Count and Say的更多相关文章
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- C#中Length和Count的区别(个人观点)
这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...
- [PHP源码阅读]count函数
在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
- 学习笔记 MYSQL报错注入(count()、rand()、group by)
首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...
- count(*) 与count (字段名)的区别
count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The counts array has ...
随机推荐
- OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)
公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ...
- [持续更新] 文章列表 last updated SEP 18, 2016
1.前端 HTML5快速学习二 Canvas@20141125 HTML5快速学习一@20141122 2.ASP.NET(MVC) MVC5+EF6 入门完整教程14--动态生成面包屑@201608 ...
- 【Bootstrap基础学习】02 Bootstrap的布局组件应用示例
字体图标的应用示例 <button type="button" class="btn btn-default"> <span class=&q ...
- csharp: WebBrowser read baidumap
setpoint.html: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Typ ...
- C#枚举类型和结构体
注意:枚举类型和结构体都属于值类型. 结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的方法: struct student { public int nianl ...
- 2013学习总结----JavaScript
javascript面向对象,实现的几种方式 1:直接使用JSON对象 var o1={ "a":1, "b":2, "c":functio ...
- GitHub上我收藏Java及Android的项目Demo
接触编程不久但浏览频率最高的还是GitHub毕竟它真的是程序员必不可少的新世界. (2016/9/23更新) 静态更新,很强势你值得拥有 更新资源不需要重新安装APK (2016.9.10更新) ht ...
- ORA-00257归档日志写满的解决方法
背景: 在前一篇博客中我们提到了如何启动或关闭oracle的归档(ARCHIVELOG)模式,在我成功设定数据库为归档模式以后, 第二天再次尝试连接数据库,报错:ORA-00257.在网上找到了一圈资 ...
- mac java 环境设置
MAC下JDK1.6下载路径 http://support.apple.com/kb/DL1572 Mac OS的java版本问题和Eclipse中无法找到jdk源代码的问题解决办法 下载包含源代码j ...
- 基础学习day10--异常、包
一.异常 1.1.异常定义 异常:--不正常,程序在运行时出现不正常情况 异常由来:其实也是现实生活中一个具体的事物,马可以通过JAVA的类的形式表现描述,并封装成类. Java对不正常情况描述后的, ...