LeetCode 38. Count and Say(点数)
来源:互联网 发布:工控老鬼如何编程 编辑:程序博客网 时间:2024/05/08 03:32
原题网址:https://leetcode.com/problems/count-and-say/
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) { List<Integer> nums = new ArrayList<>(); nums.add(1); for(int i=1; i<n; i++) { List<Integer> counts = new ArrayList<>(); List<Integer> numbers = new ArrayList<>(); for(int j=0; j<nums.size(); j++) { if (j == 0 || !numbers.get(numbers.size()-1).equals(nums.get(j))) { numbers.add(nums.get(j)); counts.add(1); } else { counts.set(counts.size()-1, counts.get(counts.size()-1)+1); } } nums = new ArrayList<>(); for(int j=0; j<numbers.size(); j++) { nums.add(counts.get(j)); nums.add(numbers.get(j)); } } StringBuilder sb = new StringBuilder(); for(int i=0; i<nums.size(); i++) sb.append(nums.get(i)); return sb.toString(); }}
0 0
- LeetCode 38. Count and Say(点数)
- Leetcode:38. Count and Say(JAVA)
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- Leetcode 38. Count and Say
- leetcode 38. Count and Say
- [leetcode] 38. Count and Say
- LeetCode-38.Count and Say
- LeetCode *** 38. Count and Say
- leetcode 38. Count and Say
- LeetCode-38. Count and Say
- LeetCode 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode - 38. Count and Say
- LeetCode 38. Count and Say
- uva10605 最大公共子序列转化为最大上升子序列
- Nsstring中搜索方法rangeOfString 字符串查找
- c#获取系统语言及版本信息
- 硬件中断处理相关的杂记
- Python中装饰器
- LeetCode 38. Count and Say(点数)
- python 解析 json文件
- COleVariant与常用基本数据类型之间的转换
- 四相五线步进电机
- Eclipse 控制台不显示打印信息的处理方法
- pip缓存下载自http链接的包
- 【41】递增数组判断是否存在两个树等于给定值
- 天道酬勤,妹子在粤嵌找到自己的目标
- HTML路径 href路径