当前位置: 首页 > news >正文

Python每日一练(20230330)

目录

1. 存在重复元素  🌟

2. 矩阵置零  🌟🌟

3. 回文对  🌟🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


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 = 0while count < len(nums) - 1:if nums[count] == nums[count + 1]:return Truecount += 1return False

输出:


2. 矩阵置零

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

进阶:

  • 一个直观的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
  • 你能想出一个仅使用常量空间的解决方案吗?

示例 1:

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

示例 2:

输入: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:returnm = len(matrix)if m == 0:returnr = []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] = 0for i in range(m):for j in c:matrix[i][j] = 0return 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 rethash_map = {}for i in range(len(words)):word = words[i][::-1]hash_map[word] = ires = []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 ress = Solution()
words = ["abcd","dcba","lls","s","sssll"]
print(s.palindromePairs(words))

输出:

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


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

http://www.lryc.cn/news/44020.html

相关文章:

  • 面试官:Tomcat 在 SpringBoot 中是如何启动的(二)
  • 软件测试岗位中,如何顺利拿下50K+?送你一份涨薪秘籍
  • java webflux函数式实现数据结构
  • 百度文心一言可以完胜ChatGPT的4点可能性
  • 大型分布式架构设计
  • 基于springboot实现校园在线拍卖电商系统【源码】
  • SaaS智慧校园源码,电子班牌管理系统 人脸考勤、综合评价系统、请假管理、校务管理
  • MONGODB mongodb 一般人不知道的数据类型与使用
  • 蚁群算法优化
  • 山东首版次申报的材料
  • 个人时间管理网站—首页的前端实现【源码】
  • Python毕业设计推荐
  • 使用nodemon时报错:“无法加载文件...,因为在此系统上禁止运行脚本“;windows执行策略修改
  • 网络协议分析期末复习(五)
  • 外贸找客户软件:Yellow Page Spider 8.7.1 Crack
  • 博客管理系统(前端页面设计)
  • 安装yolov5环境
  • IP 归属地查询 API 教你从0到1顺着网线找到键盘侠
  • 【K8S系列】深入解析Pod对象(二)
  • 从3千到3万,我的测试之路真的坎坷
  • linux下使用system函数在程序中运行linux的shell命令
  • 银行数字化转型导师坚鹏:银行业发展趋势及对人才的需求分析
  • NFS挂载
  • IDEA使用技巧
  • 自动化测试之一【接口测试总结】
  • 科大奥瑞物理实验——傅里叶光学
  • mysql count(*)的性能如何?
  • gan实战(基础GAN、DCGAN)
  • 使用C语言实现服务器/客户端的TCP通信
  • AI模型训练推理一定要知道的事情