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 n
th 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 ...
随机推荐
- CGContextRef 用法总结
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文1 CGContextMoveToPoint 开始画线2 CGContext ...
- DPDK中使用VFIO的配置
VFIO VFIO是一个可以安全地把设备I/O.中断.DMA等暴露到用户空间(userspace),从而可以在用户空间完成设备驱动的框架.用户空间直接设备访问,虚拟机设备分配可以获得更高的IO性能. ...
- 安装及使用supervisor
用途有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候,希望能自动重新启动它.此时,我就需要使用到了Supervisor. 前言supervisor管理的进程 ...
- CoreAnimation|动画
IOS开发UI篇--IOS动画(Core Animation)总结 - CSDN博客 iOS动画,绝对够分量! - 简书 iOS动画篇:UIView动画 - 简书 iOS动画开发之五--炫酷的粒子效果 ...
- BZOJ 2654: tree(二分 最小生成树)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2901 Solved: 1196[Submit][Status][Discuss] Descript ...
- mysql如何把一个表直接拷贝到一个新的表
一:在新表已经建立好的情况下 1,拷贝所有的字段 insert into new_table select * from old_table 2,拷贝部分字段表 insert into new_tab ...
- Linux安装部署
Linux桌面发行版 UbuntuCentOSRed heat LinuxOracle Linux 一.系统安装 1.系统分区 若手动分区swap和根分区必须创建,推荐创建boot分区. /----- ...
- Yar请求数据接口
//[['u'=>'site.index','d'=>['a'=>2],'k'=>'test']]; public function apiBatch($arr,$timeou ...
- PHP Ajax跨域问题解决办法
在项目开发中,经常会遇到跨域访问资源,上传图片等,那么这些都怎么解决呢,下面简单介绍一下ajax请求时,解决跨域问题. 原文地址:小时刻个人博客 > http://small.aiweimeng ...
- 大数据学习:Spark是什么,如何用Spark进行数据分析
给大家分享一下Spark是什么?如何用Spark进行数据分析,对大数据感兴趣的小伙伴就随着小编一起来了解一下吧. 大数据在线学习 什么是Apache Spark? Apache Spark是一 ...