0216leetcode刷题python5道

38

题目描述:
给定一个正整数 n ,输出外观数列的第 n 项。
「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
你可以将其视作是由递归公式定义的数字字符串序列:
countAndSay(1) = “1”
countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。
前五项如下:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 111221
    

第一项是数字 1
描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 “11”
描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 “21”
描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 “1211”
描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 “111221”

要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。

示例:
在这里插入图片描述
解答:

class Solution:
    def countAndSay(self, n: int) -> str:
        #迭代方法
        s = "1"
        for i in range(n - 1):
            t = ""
            i, j, count = 0, len(s), 1
            while i < j - 1:
                if s[i] == s[i + 1]:
                    count += 1
                    i += 1
                else:
                    t = t + str(count) + s[i]
                    count = 1
                    i += 1
            s = t + str(count) + s[i]
        return s

59

题目描述:
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

示例:
在这里插入图片描述
解答:

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        maxtrix = [[0] * n for _ in range(n)]

        x_min, x_max = 0, n - 1
        y_min, y_max = 0, n - 1
        x, y = 0, 0
        dx, dy = 0, 1
        for i in range(n * n):
            maxtrix[x][y] = i + 1

            if y + dy > y_max:  # top right
                x_min += 1
                dx, dy = 1, 0
            elif x + dx > x_max:  # bottom right
                y_max -= 1
                dx, dy = 0, -1
            elif y + dy < y_min:  # bottom left
                x_max -= 1
                dx, dy = -1, 0
            elif x + dx < x_min:  # top left
                y_min += 1
                dx, dy = 0, 1
                
            x += dx
            y += dy

        return maxtrix

146

题目描述:
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。
实现 LRUCache 类:
LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

示例:
在这里插入图片描述
解答:

class LRUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.cache = {
    
    }


    def get(self, key: int) -> int:
        # 搜索不到返回-1
        if key not in self.cache:
            return -1
        # 取出缓存中的key并赋值
        self.cache[key] = self.cache.pop(key)
        return self.cache[key]


    def put(self, key: int, value: int) -> None:
        # 如存在就先删除key
        if key in self.cache:
            self.cache.pop(key)
        self.cache[key] = value
       # 取缓存列表中最先进入的key
        if len(self.cache) > self.capacity:
            x = list(self.cache)[0]
            self.cache.pop(x)



# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)

485

题目描述:
给定一个二进制数组, 计算其中最大连续1的个数。

示例:
在这里插入图片描述
解答:

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        cnt,res=0,0
        for i in nums:
            if i==1:
                cnt+=1
                res=max(cnt,res)
            else:
                cnt=0
        return res

561

题目描述:
给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和最大。
返回该 最大总和 。

示例:
在这里插入图片描述
解答:

class Solution:
    def arrayPairSum(self, nums: List[int]) -> int:
        return sum(sorted(nums)[::2])

猜你喜欢

转载自blog.csdn.net/yeqing1997/article/details/113813532