leetcode Count and say
来源:互联网 发布:淘宝下载器 编辑:程序博客网 时间:2024/05/10 08:22
38. Count and Say
My SubmissionsTotal Accepted: 77548 Total Submissions: 269947 Difficulty: Easy
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.
Subscribe to see which companies asked this question
public class Solution { public String countAndSay(int n) { String ans="1"; for(int i = 1; i < n; i++){ ans = read(ans); } return ans; }
public String read(String s){ int len = s.length(); StringBuilder builder = new StringBuilder(128); char pri = s.charAt(0),now; int count = 1; for(int i = 1; i < len;i++){ now = s.charAt(i); if(pri == now){ count++; } else{ builder.append(count); builder.append(pri); pri = now; count = 1; } } builder.append(count); builder.append(s.charAt(len-1)); return builder.toString(); }}
//stringbuilder 还要学习下
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
- javascript的函数作用域及声明提前
- 20160409
- dubbox 的各种管理和监管
- 栈溢出笔记1.5 换一个汇编工具
- java线程
- leetcode Count and say
- 调试记录:The public type <<classname>> must be defined in its own file
- 各种排序算法
- 二叉树的层次遍历
- Apache 禁止访问
- nyoj 12 喷水装置(二)
- HDU 1583 DNA Assembly(暴力模拟)
- Ubuntu自己下的firefox无法固定到启动器且无法显示图标解决方法
- 常用的基本进制转换与自动装箱拆箱