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 ...
随机推荐
- vlc源码分析(三) 调用live555接收RTSP数据
首先了解RTSP/RTP/RTCP相关概念,尤其是了解RTP协议:RTP与RTCP协议介绍(转载). vlc使用模块加载机制调用live555,调用live555的文件是live555.cpp. 一. ...
- SSM衍生的配置文件
JDBC:(Java database connectivity) 目的:将Java语言和数据库解耦和,使得一套Java程序能对应不同的数据库. 方法:sun公司制定了一套连接数据库的接口(API). ...
- Angular7教程-05-搭建项目环境
1. 本节说明 本节以及后面的内容我们将会通过搭建一个简单的博客程序来对angular进行介绍,项目使用前端框架是bootstrap.版本v3.3.7,另外需要安装jquery.关于bootstrap ...
- mysql 5.7设置密码无效
我现在MySQL的版本时8.0.12,以前一直没有给MySQL设置密码. 今天因为需要,给MySQL设置,密码,但是上网搜了好久.....命令都不对.最后搜到csdn的Bpazy大佬的博客.他使用5. ...
- Sppring MVC核心应用-2
一.Spring MVC框架中400状态码的请求错误:控制台BindException异常, 解决方法: 二.Sping 表单标签 三.数据校验 实现JSR 303验证步骤 四.REST风格 五.Sp ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- WebSocket 客户端实例
Node.js var ws = require("ws"); var socket = new ws("ws://127.0.0.1:8001); var socket ...
- linux不重启挂载磁盘安装grub
挂载.分区.grub 通过给一块新磁盘安装grub回顾磁盘挂载.分区文件系统创建等操作: 该实验基于(CtonOS6.8:kernel:2.6.32-642.15.1.el6.x86_64) 1.通过 ...
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- MySQL事务异常
在做大屏系统的时候,遇到十分奇怪的问题,同样的代码,测试环境插入与更新操作正常,但是上了生产环境之后,插入与更新不生效, 插入数据的时候,主键会自增,但是查询表中没有数据,同样一个@Transacti ...