leetcode--Count and Say
来源:互联网 发布:java的tostring方法 编辑:程序博客网 时间:2024/06/05 09:52
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 class Solution { public String countAndSay(int n) { if(n<1) return ""; String str = "1"; while(--n>0){ StringBuilder sb = new StringBuilder(); char[] arr = str.toCharArray(); char pre = arr[0]; int count = 1; for(int i=1;i<arr.length;i++){ if(pre!=arr[i]){ sb.append(count).append(pre); count = 1; pre = arr[i]; }else{ count++; } } sb.append(count).append(pre); str = sb.toString(); } return str; }}
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- Android多屏幕适配学习笔记
- 前端精选文摘:BFC 神奇背后的原理
- iOS8 下定位失败问题
- ganglia安装教程(centos7)
- 关于SQL的基础知识
- leetcode--Count and Say
- C++ Pirmer 读书笔记-第二章
- ubuntu 设置网络
- Renginx服务器basePath路径问题如何解决
- Fragment
- C++ Primer 读书笔记 – 第三章
- with grant option与with admin option区别
- 结构体内存对齐规则
- linux 系统监控、诊断工具之 IO wait