Python每日一练(20230330)

简介: Python每日一练(20230330)

1. 存在重复元素


给定一个整数数组,判断是否存在重复元素。


如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false

示例 1:

输入: [1,2,3,1]

输出: true


示例 2:

输入: [1,2,3,4]

输出: false


示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]

输出: true


出处:

https://edu.csdn.net/practice/24183905

代码:

from typing import List
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        nums.sort()
        count = 0
        while count < len(nums) - 1:
            if nums[count] == nums[count + 1]:
                return True
            count += 1
        return False


输出:


2. 矩阵置零


给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。


进阶:

   一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。

   一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。

   你能想出一个仅使用常量空间的解决方案吗?


示例 1:

a9dfa884d1a855d6630087aa890a20f8.jpeg


输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]

输出:[[1,0,1],[0,0,0],[1,0,1]]


示例 2:


816bcf700b7a5b0c1b118f9aaac5bb58.jpeg


输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]

输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]


提示:

   m == matrix.length

   n == matrix[0].length

   1 <= m, n <= 200

   -2^31 <= matrix[i][j] <= 2^31 - 1


出处:

https://edu.csdn.net/practice/24183904

代码:


class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        if not matrix:
            return
        m = len(matrix)
        if m == 0:
            return
        r = []
        c = []
        n = len(matrix[0])
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    r.append(i)
                    c.append(j)
        r = set(r)
        c = set(c)
        for i in r:
            for j in range(n):
                matrix[i][j] = 0
        for i in range(m):
            for j in c:
                matrix[i][j] = 0
        return matrix
# %%
s = Solution()
print(s.setZeroes(matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]))


输出:

[[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]


3. 回文对


给定一组 互不相同 的单词, 找出所有 不同 的索引对 (i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。


示例 1:

输入:words = ["abcd","dcba","lls","s","sssll"]

输出:[[0,1],[1,0],[3,2],[2,4]]  

解释:可拼接成的回文串为 ["dcbaabcd","abcddcba","slls","llssssll"]


示例 2:

输入:words = ["bat","tab","cat"]

输出:[[0,1],[1,0]]  

解释:可拼接成的回文串为 ["battab","tabbat"]


示例 3:

输入:words = ["a",""]

输出:[[0,1],[1,0]]


提示:

   1 <= words.length <= 5000

   0 <= words[i].length <= 300

   words[i] 由小写英文字母组成


出处:

https://edu.csdn.net/practice/24183903

代码:


from typing import List
class Solution:
    def palindromePairs(self, words: List[str]) -> List[List[int]]:
        def is_palindrome(str, start, end):
            """检查子串是否是回文串
            """
            part_word = str[start : end + 1]
            return part_word == part_word[::-1]
        def find_reversed_word(str, start, end):
            """查找子串是否在哈希表中
            Return:
                不在哈希表中,返回 -1
                否则返回对应的索引
            """
            part_word = str[start : end + 1]
            ret = hash_map.get(part_word, -1)
            return ret
        hash_map = {}
        for i in range(len(words)):
            word = words[i][::-1]
            hash_map[word] = i
        res = []
        for i in range(len(words)):
            word = words[i]
            word_len = len(word)
            if is_palindrome(word, 0, word_len - 1) and "" in hash_map and word != "":
                res.append([hash_map.get(""), i])
            for j in range(word_len):
                if is_palindrome(word, j, word_len - 1):
                    left_part_index = find_reversed_word(word, 0, j - 1)
                    if left_part_index != -1 and left_part_index != i:
                        res.append([i, left_part_index])
                if is_palindrome(word, 0, j - 1):
                    right_part_index = find_reversed_word(word, j, word_len - 1)
                    if right_part_index != -1 and right_part_index != i:
                        res.append([right_part_index, i])
        return res
s = Solution()
words = ["abcd","dcba","lls","s","sssll"]
print(s.palindromePairs(words))




输出:

[[1, 0], [0, 1], [3, 2], [2, 4]]






目录
相关文章
|
Python 人工智能
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
247 1
讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?
|
Shell Unix Linux
Linux 终端命令之文件浏览(3) less
Linux 终端命令之文件浏览(3) less
173 0
Linux 终端命令之文件浏览(3) less
|
Rust
Rust 编程小技巧摘选(8)
Rust 编程小技巧摘选(8)
373 0
Rust 编程小技巧摘选(8)
|
算法 C++ 机器人
力扣 C++|一题多解之动态规划专题(1)
力扣 C++|一题多解之动态规划专题(1)
131 0
力扣 C++|一题多解之动态规划专题(1)
|
C++ Python 索引
Python Numpy入门基础(二)数组操作
Python Numpy入门基础(二)数组操作
187 0
Python Numpy入门基础(二)数组操作
|
C++ 存储
力扣C++|一题多解之数学题专场(1)
力扣C++|一题多解之数学题专场(1)
162 0
力扣C++|一题多解之数学题专场(1)
|
Java Go C++
Golang每日一练(leetDay0118) 扁平化嵌套列表迭代器、整数拆分
Golang每日一练(leetDay0118) 扁平化嵌套列表迭代器、整数拆分
167 0
Golang每日一练(leetDay0118) 扁平化嵌套列表迭代器、整数拆分
|
Java Go C++
Golang每日一练(leetDay0114) 矩阵中的最长递增路径、按要求补齐数组
Golang每日一练(leetDay0114) 矩阵中的最长递增路径、按要求补齐数组
194 0
Golang每日一练(leetDay0114) 矩阵中的最长递增路径、按要求补齐数组
|
Java Go C++
Golang每日一练(leetDay0110) 零钱兑换I\II Coin Change
Golang每日一练(leetDay0110) 零钱兑换I\II Coin Change
174 0
Golang每日一练(leetDay0110) 零钱兑换I\II Coin Change
|
Java Go Rust
Rust每日一练(Leetday0030) 合并有序数组、格雷编码、子集II
Rust每日一练(Leetday0030) 合并有序数组、格雷编码、子集II
163 0
Rust每日一练(Leetday0030) 合并有序数组、格雷编码、子集II

推荐镜像

更多