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

好奇怪的一道题目,意思是数(三)数(四).把数字用口头语言说出来,1就是1,2前面是1就是1一个(11),3前面2个1就是(21),然后是(1211),再是(111221)以此类推。。。 当时题目看了半天没看懂,又去别的地方查了下题目是什么意思才知道了:

 1 class Solution {
 2 public:
 3     string countAndSay(int n) {
 4         string curr = "";
 5         if(n <= 0) return curr;
 6         curr = "1";
 7         for(int i = 1; i < n; ++i){
 8             curr = convert(curr);
 9         }
10         return curr;
11     }
12
13     string convert(const string & prev){
14         //string result = "";
15         stringstream result;
16         char last = prev[0];
17         int count = 0;
18         int sz = prev.size();
19         for(int i = 0; i <= sz; ++i){//注意是<=
20             if(prev[i] == last)
21                 count++;
22             else{
23                 result << count << last;
24                 // result.append(count); 这里append无法实现,因为找不到itoa,很蛋疼,只能用stream来实现
25                 // result.append(last);
26                 last = prev[i];
27                 count = 1;
28             }
29         }
30         return result.str();
31     }
32 };
时间: 2024-10-15 03:43:47

LeetCode OJ:Count and Say(数数)的相关文章

LeetCode OJ Count Primes

Description: Count the number of prime numbers less than a non-negative number, n click to show more hints. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. 素数筛选法. int countPrimes(int n) { bool * IsPrime = (bo

[LeetCode] 248. Strobogrammatic Number III 对称数III

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "50&qu

【华为OJ】【060-自守数】

[华为OJ][算法总篇章] [华为OJ][060-自守数] [工程下载] 题目描述 自守数是指一个数的平方的尾数等于该数自身的自然数.例如:252 = 625,762 = 5776,93762 = 87909376. 请求出n以内的自守数的个数 接口说明 /** * 功能: 求出n以内的自守数的个数 * * 输入参数:int n * 返回值:n以内自守数的数量. */ public static int calcAutomorphicNumbers(int n) { /*在这里实现功能*/ re

【LeetCode】- Two Sum(两数相加)

[ 问题: ] Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

[LeetCode] 247. Strobogrammatic Number II 对称数II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all strobogrammatic numbers that are of length = n. For example,Given n = 2, return ["11","69","88","96"

LeetCode:最接近的三数之和【16】

LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 题目分析 这道题就是三数之和问题的一种变形. 三数之和问题的求解策略是将三指针变为双指针问题

[LeetCode] 260. Single Number III 单独数 III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

[LeetCode] 509. 斐波那契数

传送门:[LeetCode] 509. 斐波那契数 题目描述 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 给定 N,计算 F(N). 示例 1: 输入:2 输出:1 解释:F(2) = F(1) + F(0) = 1 + 0 = 1. 示例 2: 输入:3 输出:2 解释:F(3) = F

Leetcode(9)回文数

Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4MB 效果:还行 class Solution: def isPalindrome(self, x: int) -> bool: s=str(x) if s==s[::-1]: return True else: return False 第二种方法:反转一半数字 执行用时:156 ms: 内存消耗

leetcode之数组中找两数和为指定值

题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t