leetcode第38题:报数

这是一道简单题,但是我做了很久,主要难度在读题和理解题上。

思路:给定一个数字,返回这个数字报数数列。我们可以通过从1开始,不断扩展到n的数列。数列的值为前一个数列的count+num,所以我们不断叠加来完成。

class Solution:
    def countAndSay(self, n: int) -> str:
        # 第一个值直接赋予
        pre_num = "1"
        for i in range(1,n):
            # 用next_num 记录当前序列,num为前一个序列的第一个值
            next_num,num,count="",pre_num[0],1
            for j in range(1,len(pre_num)):
                # 如果num和前一个序列的下一个值相等,则将count +1
                if num==pre_num[j]:
                    count+=1
                else:
                # 如果不等,将当前count +num 写入next_num,并将count置为1,
                # num取 当前pre_num的值
                    next_num += str(count) + num
                    count =1
                    num =pre_num[j]
            next_num += str(count) + num
            pre_num = next_num
        return pre_num
            

原文地址:https://www.cnblogs.com/cchenyang/p/11568712.html

时间: 2024-10-10 21:53:56

leetcode第38题:报数的相关文章

leetcode第38题--Combination Sum

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

leetcode 第38题

这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个1     2个1     1个2,1个1   1个1,1个2,2个1  3个1,2个2,1个1 依次类推 题目很简单,但是为了得到较好的结果,还是纠结了一段时间 public class countAndSay { public String countAndSay(int n) { String num

leetcode第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

[LeetCode] Combinations [38]

题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 原题链接(点我) 解题思路 组合问题,老思路---递归加循环,这个是组合里面比较简单的. 代码实现 class Solutio

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su