class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ s=' for i in range(2,n+1): s=self.count(s) return s def count(self,s): t='';count=0;curr='#' for i in s: if i != curr: if curr != '#': t+=str(…
LeetCode初级算法的Python实现--字符串 # 反转字符串 def reverseString(s): return s[::-1] # 颠倒数字 def reverse(x): if x < 0: flag = -2 ** 31 result = -1 * int(str(x)[1:][::-1]) if result < flag: return 0 else: return result else: flag = 2 ** 31 - 1 result = int(str(x)[…
一次AC 字符串就是:count+char class Solution: # @return a string def countAndSay(self, n): str = " for i in range(n-1): tmp = str str = "" c = tmp[0] cnt = 1 for j in range(1,len(tmp)): if tmp[j] == tmp[j-1]: cnt += 1 else: str += ("%d"%c…
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…
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 of O(n2) is trivial.…