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】的更多相关文章

  1. 365. Count 1 in Binary【LintCode java】

    Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return  ...

  2. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  3. 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 ...

  4. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  5. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  6. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  7. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  8. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  9. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

随机推荐

  1. TCP连接三次握手协议,释放连接四次挥手,以及使用 awl伪造mac地址进行多线程syn洪泛攻击。

    这个TCP连接就是一次追女生-谈恋爱-分手,追求比分手简单,但是分手比追求复杂.哥,谈了半年的女朋友,在就快要成功了的时候分了,原因是因为有人在后面该老子背后搞SYN洪泛攻击,最后女朋友丢失了.学会T ...

  2. linux中安装jdk以及eclipse的安装

    最近将系统换成了linux(ubuntu14.04),随之而来的是各种软件的配置,环境的配置,因此趁机将自己的过程整理出来. 1:linux中怎么安装jdk 1 首先现在jdk源文件http://ww ...

  3. JUnit提供测试框架的优势(JUnit Provides Advantages as a Test Framework)

    测试Java类的内部功能就是刚才你做的那些工作了.真正的测试和刚才的简单例子的主要区别是代码库的大小和复杂度.在处理一大堆代码时,你会需要收集情况报告.但上面的例子遇到第一个错误就停止了,它没有收集尽 ...

  4. LUA IO库

    I/O库为文件操作提供两种模式. 简单模式(simple model)拥有一个当前输入文件和一个当前输出文件.而且提供针对这些文件相关的操作.全然模式(complete model)使用外部的文件句柄 ...

  5. C#设计模式 —— 工厂模式

    . 工厂模式同样是项目中最常用的设计模式,工厂模式中又分为简单工厂,工厂方法,抽象工厂.下面我们由简单的开始逐一介绍. 1.简单工厂模式 简单工厂又被称为静态工厂,在设计模式中属于创建型模式.主要解决 ...

  6. JAVA项目服务器部署

    1.下载 Java JDK 搜索jdk下载,然后进入JAVA官方网站jdk下载页,选择自己的对应的操作系统,点击下载 https://www.oracle.com/technetwork/java/j ...

  7. angular路由传参和获取路由参数的方法

    1.首先是需要导入的模块 import { Router } from "@angular/router";//路由传参用到 import{ActivatedRoute,Param ...

  8. Framwork框架日志与配置工具的使用

    一.使用设置: 头文件的添加: ..\Framwork\Include\pthread_64; ..\Framwork\CommFramwork\include; ..\Framwork\Utilit ...

  9. 使用Mybatis连接到Mysql报错,WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be esta

    在Eclipse中使用springboot整合Mybatis,连接到5.7版本Mysql报错WARN: Establishing SSL connection without server's ide ...

  10. centos7开机不进入图形界面

    centOS7开机不进入图形界面设置和centOS6系列不同的是,不再是直接改文件中的5就可以了. centOS7设置如下: systemctl get-default    //获取当前的默认tar ...