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 ...
随机推荐
- 2017年秋招美团Java程序员开发,看我如何拿到offer
本人是一名本科毕业非计算机专业的程序员,面了阿里,结果没过,最后面上了美团,热乎乎的面经,昨天面的美团,虽然面完了HR面,但是感觉希望不大,希望能走运拿到offer吧.记性不是太好,有一些问题没能记住 ...
- 在金融服务计算中,必须要使用BigDecimal
在Java程序开发过程中,比较初级(工作经验受限)的开发人员,把注意力全部放在了一些高大上的新技术中,往往忽略了一些初级问题.. 金融服务系统中,对金额的敏感至关重要,账户余额.还款金额.代收本金.代 ...
- 如何设计处优秀的Restful API
只知道遵规循矩的程序员是假程序员,任何技术都是不断发明创造改进的. 如何设计处优秀的Restful API? 盲目跟风,设计糟糕的Resful API = 浪费时间 ! 不啰嗦,直接进入技术主题: ...
- 【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)
Description In country Light Tower, a presidential election is going on. There are two candidates, ...
- MySQL学习【第九篇存储引擎】
一.存储引擎介绍 1.我们知道mysql程序构成由连接层,sql层,存储引擎层.存储引擎层和磁盘进行交互,由其去取数据,而我们取得数据是表的形式展现出来,谁做的呢?就是存储引擎结构化成表的形式返回给用 ...
- 由object元素引出的事件注册问题和层级显示问题
项目有一个双击监控视频全屏的需求,视频播放使用的是IE下的ActiveX控件,web页面中使用HTML嵌入对象元素object.预期方案如下: 1.在开发ActiveX控件时加入双击事件. 2.通过d ...
- node从搭建运行项目整体流程
1. 初始化配置基本信息: npm init (自定义配置) npm init -y (一切配置采用默认值) 在当前目录产生package.json文件,有一个dependencies用来记录该项目所 ...
- 20155231 java实验一 Java开发环境的熟悉
20155231 java实验一 Java开发环境的熟悉 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 完成实验.撰写实验 ...
- 20155311《Java程序设计》实验五(网络编程与安全)实验报告
20155311<Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面 ...
- 学号20155311 2016-2017-2 《Java程序设计》第一次实验报告
课程:Java程序设计 实验名称:Java开发环境的熟悉 实验目的与要求: 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 完成实验. ...