leetcode:count and say
来源:互联网 发布:淘宝中老年毛呢外套 编辑:程序博客网 时间:2024/06/07 03:21
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) { String result = "1"; if(n==1) return result; int level= 1; for(int i =2;i <= n;i++){ char first = '0'; int count = 0; StringBuilder ss = new StringBuilder(); for(int j = 0;j<result.length();j++){ char ch = result.charAt(j); if(count != 0 && ch == first){ count++; } else { if(count != 0 ){ ss.append(count); ss.append(first); } first = ch; count = 1; } } ss.append(count); ss.append(first); result = ss.toString(); } return result; }}
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
- Django 静态文件配置
- linux中某个端口拒绝远程主机连接原因及解决方法
- CentOS下JDK安装
- 购买 easy题解
- 浅析Spring中的注解
- leetcode:count and say
- Elasticsearch-PHP 快速开始
- Unity脚本中使用NGUI的预先准备
- C++STL 迭代器
- 零基础学python-18.3 递归函数vs循环和递归处理任意结构
- Hibernate: Understanding Lazy Fetching
- php实例-注册&登录
- 文章标题
- 提取神经网络数学表达式