LeetCode——Count and Say

Description:

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 static String[] str = new String[1000000];
	public static void ans() {
		str[1] = "1";
		for(int i=1; i<30; i++) {
			str[i+1] = resolve(str[i]);
		}
	}

    public static String countAndSay(int n) {
        ans();
        return str[n];
    }

	public static String resolve(String str) {
		StringBuilder res = new StringBuilder();
		for(int i=0; i<str.length(); ) {
			char cur = str.charAt(i);
			int count = 1;
			for(int j=i+1; j<str.length(); j++) {
				if(cur == str.charAt(j)) {
					count ++;
				}
				else {
					break;
				}
			}

			res.append(count+"");
			res.append(cur);
			i += count;
		}
		return res.toString();
	}
}
时间: 2024-11-03 22:07:13

LeetCode——Count and Say的相关文章

LeetCode: Count and Say [037]

[题目] 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. G

LeetCode: 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

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 retur

[LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description You are given an integer array nums and you have to return a new counts array. The counts array has the property where $counts[i]$

LeetCode -- Count Digit One

题目描述: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本题目纯粹是找规律求解. 该实现参考了:

[LeetCode] Count Univalue Subtrees 计数相同值子树的个数

Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST

LeetCode Count and Say 数数字

1 class Solution { 2 public: 3 string countAndSay(int n) { 4 if(n==1) return "1"; 5 string str0="",str1="1"; 6 int i,t,count; 7 char c='*'; 8 for(i=0;i<n-1;i++){ //一共要数n-1次,假如n=2,那么只要数str1这一次就行了 9 count=1; 10 if(i%2!=0){ /