【LeetCode从零单排】No38.CountAndSay
题目
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.
Note: The sequence of integers will be represented as a string.
代码
public String countAndSay(int n){String lastNum = "1";for(int i = 1; i <= n - 1 ; i++){StringBuilder sb = new StringBuilder();char[] chars = lastNum.toCharArray();char cur = chars[0];int times = 0;for(int j = 0; j < lastNum.length(); j++){if(chars[j] == cur)times ++;else {sb.append(times).append(cur);cur = chars[j];times = 1;}if(j == lastNum.length() - 1){sb.append(times).append(cur);}}lastNum = sb.toString();}return lastNum;}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No38.CountAndSay相关推荐
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode从零单排】No221.Maximal Square
题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
最新文章
- MVC URL参数传递+变为空格解决方法
- [iOS] dom解析xml数据,拿到lt;gt;里面的值
- spring html访问统计,spring-mvc访问本地html文件(示例代码)
- java mail 已发送_JavaMail获取已发送邮件
- 从五大结构体,带你掌握鸿蒙轻内核动态内存Dynamic Memory
- 数据:以太坊2.0合约余额新增1.28万ETH
- 连接远程hbase长时间等待问题
- 新时代、新挑战、新机遇
- hdu 4405 Aeroplane chess 概率dp
- (结构型模式)FlyWeight——享元模式
- 227.基本计算器II
- 循环冗余校验(CRC,模2运算)
- static关键字的用法
- 蚂蚁区块链BaaS平台应用开发指南(一):前言
- Python模拟post提交表单数据 ——某二手车网站回拨电话的分析与利用
- (2020)使用Airtest来爬取某宝评论数据
- 常见乱码问题分析和总结
- 秉火429笔记之十五 DMA--直接存储区访问
- 《利用Python进行数据分析·第2版》第14章 数据分析案例
- Max Script|灯光篇
热门文章
- 2020年余丙森概率统计强化笔记-第一章 随机事件及其概率-第二章 一维随机变量及其分布
- mongodb java crud_MongoDB用Java,简单的CRUD操作,两种方法,附源码下载。
- C代码开发遇到的问题 变量初始化和结构体指针移动
- vba sub是什么缩写_这些掌握了,你才敢说自己懂VBA
- python 元组使用_Python3
- 计算机系统加固方法有哪些,计算机系统的加固 .doc
- 下c语言按q退出_Linux vim编辑器怎么退出?第一次用都会问的问题
- linux nvme分区,这些 loop 分区是什么鬼东西?
- oracle if 嵌套语句吗,Lua嵌套if语句
- kafka 启动_Kafka的安装与启动