Leetcode-count-and-say
来源:互联网 发布:mac qq代理服务器 编辑:程序博客网 时间:2024/06/07 16:24
题目描述
Thecount-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is readoff as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.
Given an integer n, generate the nth sequence.
Note: Thesequence of integers will be represented as a string.
其实题目很简单的,理解起来也不难,主要是下标的操作繁琐,我自己试了两个方法,都是下标出错,最后参考别人的代码,真的觉得自己思路还是不够清晰。这一题我要背诵下来!
public class Solution { public String countAndSay(int n) { String[] data = new String[n]; data[0] = "1"; for(int i=1; i<n; i++){ String pre = data[i-1]; String res = ""; for(int j=0; j<pre.length() ; ){ int t = j; while(j < pre.length() && pre.charAt(j) == pre.charAt(t)) { j++; } res += String.valueOf(j-t)+pre.charAt(t); } data[i]= res; } return data[n-1]; }}
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
- PV,UV,VV,IP解析
- APM (应用性能管理)
- 最全面的Java面试题-----是你更好的掌握java知识
- 经过show variables like xxx 详解mysql运行时参数
- Leetcode-unique-paths
- Leetcode-count-and-say
- 如何有效的结束项目--对某税务MIS系统项目的经验总结
- android.graphics.Camera API 中文翻译
- AOE网络的算法,活动的最早开始时间,最晚结束时间,关键活动,关键路径
- 在Windows系统下,搭建最简单的nginx服务器
- 浅谈web应用的负载均衡、集群、高可用(HA)解决方案
- js实现动画,右移、左移,上移,下移,左上角移动等
- 数据库设计案例分析
- Java中Set,List,Map的区别