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 ...
随机推荐
- jQuery: jquery.json.js
http://api.jquery.com/jQuery.parseJSON/ http://www.json.org/json-zh.html http://fineui.codeplex.com/ ...
- 在windows下配置Eclipse + go环境
http://blog.csdn.net/hengyunabc/article/details/7371446 本文章地址:http://blog.csdn.net/hengyunabc/articl ...
- 无法打开Android SDK Manager的解决办法
不知道从什么时候开始,打开Android的SDK Manager.exe时,命令行窗口一闪就自动关掉了. 想更新一些Android的东西都更新不了. 查了一下,解决办法是: 环境变量的系统变量Path ...
- css样式reset
初始化样式. @CHARSET "UTF-8"; body,ur,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div, ...
- Maven创建webapp(二)
这一节将记录在myeclipse下用maven创建一个简单的webapp项目 web开发maven仓库自动添加组件,故需要需要保持网络的通畅. 打开myeclipse --> File - ...
- nginx对网站限速
注意:nginx 1.1.8 之后的版本的语法改为limit_conn_zone $binary_remote_addr zone=NAME:10m; NAME 就是 zone 的名字限制连接数:要限 ...
- PHP学习笔记:通过curl实现采集网站内容
关于curl,请各位同学自行百度,我直接上案例. 首先开启你的curl拓展,在php.ini文件把curl拓展开启,即取消extension=php_curl.dll的分号. eg:利用curl采集网 ...
- spring mvc 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...
- JS中检测数据类型的几种方式及优缺点
1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...
- ArcEngine尝试读取或写入受保护的内存
先说一下我的开发环境: Win10 + ArcGIS10.0 + ArcEngine10.0 + Framework4.0 今天调用新的GP工具则出现"尝试读取或写入受保护的内存.这通常指示 ...