420. Count and Say【LintCode java】
Description
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.
The sequence of integers will be represented as a string.
Example
Given n = 5, return "111221".
解题:这题目不太好理解,第一个数是 1,第二个数是第一个数的读法,依次类推。参考:https://www.2cto.com/kf/201502/375593.html
代码如下:
public class Solution {
/**
* @param n: the nth
* @return: the nth sequence
*/
public String countAndSay(int n) {
// write your code here
if(n == 1)
return "1";
String temp = countAndSay(n - 1) + "*";
char[]c = temp.toCharArray();
int count = 1;
String res = "";
for(int i = 0; i < c.length - 1; i++){
if(c[i] == c[i+1]){
count++;
}else{
res = res + count + c[i];
count = 1;//count归0
}
}
return res;
}
}
420. Count and Say【LintCode java】的更多相关文章
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 213. String Compression【LintCode java】
Description Implement a method to perform basic string compression using the counts of repeated char ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
随机推荐
- FLV封装格式分析
FLV官方文档:https://github.com/jiayayao/DataSheet/tree/master/media%20format/flv 一.FLV格式 FLV包括文件头(FLV he ...
- ArrayList两个对象之间的赋值
List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList&l ...
- node.js 基于cheerio的爬虫工具,需要登录权限的爬虫工具
公司有过一个需求,需要拿一个网页的的表格数据,数据量达到30w左右:为了提高工作效率. 结合自身经验和网上资料.写了一套符合自己需求的nodejs爬虫工具.也许也会适合你的. 先上代码.在做讲解 'u ...
- C++快速开发样本工程的建立--建立工程
因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...
- codeforces 979 C. Kuro and Walking Route
C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- MySQL架构与引擎初识
一.MySQL逻辑架构 1.连接层: 最上层是一些客户端和连接服务,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等. 2.服 ...
- SQL分页过多时, 如何优化
问题: 我们经常会使用到分页操作,这里有个问题,在偏移量非常大的时候,它会导致MySQL扫描大量不需要的行然后再抛弃掉.如: , ; 上述这条SQL语句需要查询10020条记录然后只返回最后20条.前 ...
- jQuery属性操作之.attr()
目录 .attr() 调用形式:$("xxx").attr(name) 调用形式:$("xxx").attr(name,value); 调用形式:$(" ...
- 解决ios下audio不能正常播放的问题
解决ios下audio不能正常播放的问题 ios系统下会自动屏蔽audio标签的自动播放,需要使用一个事件来驱动音频播放 this.$refs.startaudio.addEventListener( ...
- HIVE—数据仓库
1. hive是什么? Hive是基于 Hadoop 的一个数据仓库工具: 1. hive本身不提供数据存储功能,使用HDFS做数据存储: 2. hive也不分布式计算框架,h ...