38. 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) {
String s="1";
if(n==0||n==1)
return s;
String result="";
for(int k=1;k<n;k++) //用for循环
{
char[] ss=s.toCharArray();
result="";
int i=0;
int j=0;
for( i=0;i<ss.length;)
{
char a=ss[i];
int count=0;
for(j=i;j<ss.length;j++)
{
if(a==ss[j])
count++;
else break;
}
result=result+Integer.toString(count)+Character.toString(a);
i=j;
}
s=result;
}
return s;
}
}
方法二:
public class Solution {
public String countAndSay(int n) {
String s="1";
if(n==0||n==1)
return s;
s=countAndSay(n-1);//用递归
String result="";
char[] ss=s.toCharArray();
result="";
int i=0;
int j=0;
for( i=0;i<ss.length;)
{
char a=ss[i];
int count=0;
for(j=i;j<ss.length;j++)
{
if(a==ss[j])
count++;
else break;
}
result=result+Integer.toString(count)+Character.toString(a);
i=j;
}
return result;
}
}
38. Count and Say的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
- leetCode练题——38. Count and Say
1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...
- 【leetcode❤python】 38. Count and Say
#-*- coding: UTF-8 -*- class Solution(object): def countAndSay(self, n): """ ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- 38. Count and Say - Unsolved
https://leetcode.com/problems/count-and-say/#/description The count-and-say sequence is the sequence ...
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
随机推荐
- bzoj 2037: [Sdoi2008]Sue的小球
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...
- oracle数据库常用SQL语句(11.29更新)
笔者日常工作中常用到的sql语句,现总结如下,留作日后查看. 1.按照两列中的最大值取 ,只取两列其中的一列 SELECT * FROM t_doc T ORDER BY GREATEST(T.Loa ...
- net IL的一些探索
查看IL有2个工具比较好用,一个是大名鼎鼎的Reflector,但已经转向收费,另一个是开源的ILSpy,强大好用,对linq和lambda表达示的支持很好.相关的书籍也很多,比如这本Microsof ...
- 【STL】- vector的用法
初始化: 1. 默认构造: vector<int> vint; 2. 用包含10个元素的数组初始化: vector<int> vint(ia, ia+10); 算法: 1. v ...
- High Performance Django
构建高性能Django站点 性能 可用 伸缩 扩展 安全 build 1.审慎引入第三方库(是否活跃.是否带入query.是否容易缓存) 2.db:减少query次数 减少耗时query 减小返回 ...
- Android 系统基础
当系统启动一个组件,它其实就启动了这个程序的进程(如果这个进程还未被启动的话)并实例化这个组件所需要的类. 例如,如果你的程序启动了相机程序里的activity去拍照,这个activity实际上是运行 ...
- linux命令每日一练习-ls
ls列出目录下所有文件 ls -l列出具体信息. drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32 第一个d表示是目录,如果是-表示普 ...
- 阿里公共DNS 正式发布了
喜大普奔!集阿里巴巴集团众多优秀工程师开发维护的公共DNS---AliDNS终于上线啦!作为国内最大的互联网基础服务提供商,阿里巴巴在继承多年优秀技术的基础上,通过提供性能优异的公共DNS服务,为广大 ...
- 用php 查询显示新闻消息
创建数据库: create database mydb ; use mydb ; create table News ( ids int identity primary key, title var ...
- 关于高并发的aotomic
AtomicInteger线程安全的根源,熟悉并发的同学一定知道在java中处理并发主要有两种方式: 1,synchronized关键字,这个大家应当都各种面试和笔试中经常遇到. 2,volatile ...