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 ...
随机推荐
- 保护WIFI无线网络的安全
本篇博客属于我们隐私与安全小贴士系列博客的一部分,其目的是确保您以及您的家人的上网安全.隐私与安全问题无论对我们还是对您都至关重要.我们在“不可 不知的小知识”网站上为您提供了如何安全,便捷地使用互联 ...
- 走进AngularJs(七) 过滤器(filter) - 吕大豹
时间 2013-12-15 16:22:00 博客园-原创精华区 原文 http://www.cnblogs.com/lvdabao/p/3475426.html 主题 AngularJS 过滤器 ...
- DrawerLayout一个简单的实例(与ActionBar无关)
官方的Demo里有DrawerLayout的例子,涉及到ActionBar,这里不用ActionBar,手痒,写个超级简单的小Demo,备着以后或许会用到. 详细的内容,可以访问:http://blo ...
- Android双击返回按钮退出程序
//双击退出事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KE ...
- android 定时器的使用
1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...
- iOS之沙盒机制和如何获取沙盒路径
iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...
- Register-SPWorkflowService 远程服务器返回错误: (404) 未找到
博客地址:http://blog.csdn.net/foxdave 当想创建一个SharePoint 2013 工作流的时候,打开SharePoint 2013 Designer(一下简称SPD),发 ...
- BCP 导入导出数据库数据
使用 bcp 将数据库迁移到 Azure SQL Database --所有 都是在本机sql上运行--先开启cmdshellEXEC sp_configure 'show advanced opti ...
- URL详谈
URL(Uniform Resource Locator,统一资源定位符)是地址的别名.它包含关于文件存储位置和浏览器应如何处理它的信息.互联网上的每个文件都有唯一的 URL. URL 的第一个部分称 ...
- iOS7中计算UILabel中字符串的高度
iOS7中计算UILabel中字符串的高度 iOS7中出现了新的方法计算UILabel中根据给定的Font以及str计算UILabel的frameSize的方法.本人提供category如下: UIL ...