Count and Say (java)
来源:互联网 发布:mac svn 管理工具下载 编辑:程序博客网 时间:2024/06/05 09:48
题目:
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.
给定整数n,输出第n个序列。
理解题意:第n个序列是和第n-1个序列有关的。第n-1个序列从左往右,字母个数+字母就得到第n个序列。
public class Solution{ public String countAndSay(int n){ if(n<=0) return ""; String oldString = "1"; while(--n > 0) { StringBuilder newString = new StringBuilder(); int count=1; for(int i=1; i<oldString.length();++i) { if(oldString.charAt(i-1)==oldString.charAt(i)) { ++count; } else { newString.append(count); newString.append(oldString.charAt(i-1)); count=1;//继续下一个字母的统计 } } //最后一个字母的统计,因为for循环中处理流程:当前字母与前一个字母比较,统计的是前一个字母而不是当前字母。 newString.append(count); newString.append(oldString(charAt(oldString.length()-1))); oldString = newString.toString(); } return oldString; }}
0 0
- Count and Say Java
- Count and Say (Java)
- (java)Count and Say
- Count and Say (java)
- [Leetcode] Count and Say (Java)
- [LeetCode][Java] Count and Say
- count and say leetcode java
- LeetCode : Count and Say [java]
- LeetCode Count and Say(java)
- [leetcode-38]count and say(java)
- LeetCode 38 : Count and Say (Java)
- Leetcode:38. Count and Say(JAVA)
- [LeetCode-Java]38. Count and Say
- [leetcode]38. Count and Say(java)
- [LeetCode] 38. Count and Say java
- lintcode 报数(Count and Say) (Java)
- [leetcode]38. Count and Say@Java
- 38. Count and Say —— Java
- android 对线程池的探究
- 让html标签显示在页面上
- Python核心编程(第十三章)--面向对象编程
- Gradle学习系列之八——构建多个Project
- iOS学习- 10 定义数据模型及构造本地运行时数据 - Todo App
- Count and Say (java)
- Jenkins的部署及使用教程(插件集成sonar服务,构建过程做代码分析)
- Gradle学习系列之九——自定义Task类型
- 向maven仓库中手动安装本地jar的三种方法
- Sonar 使用详解
- iOS 基础篇8—从代码的逐步优化看MVC
- md5
- C++中ifstream使用笔记(一)(常用方法和注意事项)
- dumpsys命令用法