LeetCode 38 Count and Say(C,C++,Java,Python)
来源:互联网 发布:数控编程人员工资待遇 编辑:程序博客网 时间:2024/05/02 01:55
Problem:
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.
Solution:
依次查询每一个字符出现的次数,并统计下来,然后在结果中存入即可,题目数据量比较小。
题目大意:
根据上一个字符串的序列,来统计每个字符出现的次数和什么字符,比如第一个是1,那第二个就是对第一个字符串的描述:1个1=11
Java源代码(208ms):
public class Solution { public String countAndSay(int n) { char[] seq=new char[100000]; char[] bak=new char[100000]; char[] tmp; char t; int top=1,index,l,r,num; seq[0]='1';seq[1]=0; while(--n >0){ index=0; for(int i=0;i<top;i++){ num=1; while(i+1<top && seq[i+1]==seq[i]){i++;num++;} l=index; while(num>0){ bak[index++]=(char)(num%10+'0'); num/=10; } r=index-1; while(l<r){t=bak[l];bak[l]=bak[r];bak[r]=t;l++;r--;} bak[index++]=seq[i]; } top=index; tmp=seq;seq=bak;bak=tmp; } return new String(seq,0,top); }}
C语言源代码(0ms):
char* countAndSay(int n) { char* seq=(char*)malloc(sizeof(char)*100000); char* bak=(char*)malloc(sizeof(char)*100000); char* tmp; char t; int top=1,i,index,num,l,r; seq[0]='1';seq[1]=0; while(--n){ index=0; for(i=0;i<top;i++){ num=1; while(i+1<top && seq[i+1]==seq[i]){ i++; num++; } l=index; while(num>0){ bak[index++]=num%10+'0'; num/=10; } r=index-1; while(l<r){ t=bak[l];bak[l]=bak[r];bak[r]=t;l++;r--; } bak[index++]=seq[i]; } bak[index]=0; top=index; tmp=seq;seq=bak;bak=tmp; } free(bak); return seq;}
C++源代码(0ms):
class Solution {public: string countAndSay(int n) { char* seq=(char*)malloc(sizeof(char)*100000); char* bak=(char*)malloc(sizeof(char)*100000); char t,*tmp; int l,r,index,top=1,i,num; seq[0]='1';seq[1]=0; while(--n){ index=0; for(i=0;i<top;i++){ num=1; while(i+1<top && seq[i+1]==seq[i]){i++;num++;} l=index; while(num>0){ bak[index++]=num%10+'0'; num/=10; } r=index-1; while(l<r){t=bak[l];bak[l]=bak[r];bak[r]=t;l++;r--;} bak[index++]=seq[i]; } bak[index]=0; top=index; tmp=seq;seq=bak;bak=tmp; } return string(seq); }};
Python源代码(64ms)数据量小时可用,数据量大的话参照上面三种:
class Solution: # @param {integer} n # @return {string} def countAndSay(self, n): seq=['1'];top=1; while n-1>0: n-=1;index=0;bak=[] i=0 while i<top: num=1 while i+1<top and seq[i+1]==seq[i]:i+=1;num+=1 bak.append(chr(num+ord('0'))) bak.append(seq[i]) i+=1 seq=bak;top=len(bak) return ''.join(seq)
0 0
- LeetCode 38 Count and Say(C,C++,Java,Python)
- 【leetcode c++】38 Count and Say
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
- 【leetcode】38. Count and Say(Python & C++)
- [C++]LeetCode: 20 Count and Say
- 【C++】【LeetCode】38. Count and Say
- LeetCode c语言- Count and Say
- Leetcode-Count and Say-Python
- 【Leetcode】【python】Count and Say
- [leetcode-38]count and say(java)
- LeetCode 38 : 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]Count and Say (伯爵说 C语言实现)
- 【LeetCode算法练习(C++)】Count and Say
- zthread学习 实例十二 线程间的协助(三)——死锁
- 从零开始打造一个新闻订阅APP之爬虫篇(二、实现一个简单的爬虫系统)
- java多线程问题中死锁的一个实现
- java web 简析--学习计划
- backbone.js练习
- LeetCode 38 Count and Say(C,C++,Java,Python)
- HDU 4658 Integer Partition
- JSOI 2008 星球大战
- 黑马程序员——1,JAVA基础:认识JAVA
- iOS中的MVC设计模式
- ACM小组的古怪象棋
- Linux下Pomelo环境配置
- 教女友学PHP之HTML①
- ConcurrentHashMap原理解析