LeetCode算题——6月
2022——6月
139 单词拆分
思路: 完全不会。
class Solution(object):def wordBreak(self, s, wordDict):""":type s: str:type wordDict: List[str]:rtype: bool"""n = len(s)dp = [False] * (n + 1)dp[0] = Truefor i in range(n):for j in range(i+1,n+1):if (dp[i] and s[i:j] in wordDict):dp[j] = Truereturn dp[-1]
413 等差数列划分
思路:注意解法中的t设定的十分巧妙,形似1+2+3+4+5…
class Solution(object):def numberOfArithmeticSlices(self, nums):""":type nums: List[int]:rtype: int"""n = len(nums)if n == 1:return 0d,t = nums[0] - nums[1], 0ans = 0# 因为等差数列的长度至少为3,所以可以从i=2开始枚举for i in range(2, n):if nums[i-1] - nums[i] == d:t += 1else:d = nums[i - 1] - nums[i]t = 0ans += treturn ans
6月9号
96 不同的二叉搜索树
思路:看的题解,二叉搜索树的左子树节点数值小于根节点,右子树节点数值大于根节点。
class Solution(object):def numTrees(self, n):""":type n: int:rtype: int"""G = [0] * (n + 1)G[0], G[1] = 1, 1for i in range(2, n + 1):for j in range(1, i+1):G[i] += G[j-1] * G[i - j]return G[n]
6月17号
704 二分查找
思路: 设定low、might、high三个索引来对有序数列进行二分查找。自己写的。
class Solution(object):def search(self, nums, target):""":type nums: List[int]:type target: int:rtype: int"""low = 0high = len(nums)-1while low<=high:might = (low + high)/2if nums[might] == target:return mightelif nums[might] > target:high = might- 1else:low = might + 1return -1
278 第一个错误的版本
思路: 正确的版本左侧都是正确的,错误的版本右侧都是错误的,因此可以使用二分法检查判断次数来查找第一次错的的版本。
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):class Solution(object):def firstBadVersion(self, n):""":type n: int:rtype: int"""high = nlow = 1while low<high:might = (low + high)/2if isBadVersion(might):high = might else:low = might + 1return low
35 搜素插入位置
**思路:**利用二分法找出目标位置
class Solution(object):def searchInsert(self, nums, target):""":type nums: List[int]:type target: int:rtype: int"""h = len(nums) - 1l = 0while l <= h:m = (l+h)/2if nums[m] == target:return melif nums[m] > target:h = m - 1else:l = m + 1return l
6月18号
977 有序数组的平方
思路: 直接遍历数组进行平方,然后排序即可。自己写的
class Solution(object):def sortedSquares(self, nums):""":type nums: List[int]:rtype: List[int]"""ans = []for i in nums:ans.append(i*i)ans.sort()return ans
189 轮转数组
思路: 先将数组最后k个数据存放在另一个数组中,然后再将原数组的前(n-k)个数据往后移k个位置,最后再把存放在另一个数组中的后k个数据放回原数组的前k个位置。注意可能出现k>n的情况,因此需要在存储后k个数据之前,进行k=k%n的操作。自己写的代码
class Solution(object):def rotate(self, nums, k):""":type nums: List[int]:type k: int:rtype: None Do not return anything, modify nums in-place instead."""n = len(nums)k = k % n # 针对移位数大于数组长度的情况。ans = nums[n-k:]for i in range(n-k-1,-1,-1):nums[i+k] = nums[i]nums[:k] = ans[:]return nums
6月19号
283 移动零
思路: 使用双指针,将0每一次往后移动。没有理解。
class Solution(object):def moveZeroes(self, nums):""":type nums: List[int]:rtype: None Do not return anything, modify nums in-place instead.""" n = len(nums)left = right = 0while right < n:if nums[right] != 0:nums[left], nums[right] = nums[right], nums[left]left += 1right += 1
6月22日
1 两数之和
思路: 利用字典将数值作为键,下标作为值,先判断(target- num)是否在字典中,若在则返回两个数的下标,否则就将num插入到字典中。
class Solution(object):def twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""hashbiao = dict()for i, num in enumerate(nums):if target - num in hashbiao:return [hashbiao[target - num], i]hashbiao[nums[i]] = ireturn []
88 合并两个有序数组
思路:直接将第二个数组的元素接到第一个数组后面,然后排序。自己写的。
class Solution(object):def merge(self, nums1, m, nums2, n):""":type nums1: List[int]:type m: int:type nums2: List[int]:type n: int:rtype: None Do not return anything, modify nums1 in-place instead."""i = mj = 0while i < m+n:nums1[i] = nums2[j]i += 1j += 1nums1.sort()
6月23号
167 两数之和 II - 输入有序数组
思路: 参考《1. 两数之和》的写法,自己写的。
class Solution(object):def twoSum(self, numbers, target):""":type numbers: List[int]:type target: int:rtype: List[int]"""ans = dict()for i , num in enumerate(numbers):if target - num in ans:return [ans[target - num], i+1]ans[num] = i+1return []
6月27日
557 反转字符串中的单词 III
思路: 将字符串分割成单词列表 然后把每个单词反转切片。
class Solution(object):def reverseWords(self, s):""":type s: str:rtype: str"""words = s.split(" ")tmp = ''for i in words:tmp += i[::-1] + " "return tmp.rstrip()
方法二:先反转单词列表 再反转字符串
class Solution(object):def reverseWords(self, s):return " ".join(s.split(" ")[::-1])[::-1]
350 两个数组的交集 II
思路: 先用字典分别记录每个列表中元素的个数,然后任意遍历一个列表的元素,将同时出现在两个列表中的元素添加到新列表中,添加的个数是两个列表中元素出现次数较少的。自己写的。
class Solution(object):def intersect(self, nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: List[int]"""t1 = collections.Counter(nums1)t2 = collections.Counter(nums2)ans = []for i in nums1:if i in t1 and i in t2 and i not in ans:n = min(t1[i], t2[i])ans.extend([i]*n)return ans
LeetCode算题——6月相关推荐
- LeetCode算题——7月
7月1号 74 搜索二维矩阵 思路: 先搜素目标值可能在的行,然后判断目标值是都在改行即可.自己写的. class Solution(object):def searchMatrix(self, ma ...
- LeetCode算题准备内容
一.数据结构与算法 算法 + 数据结构 = 程序 算法(Algorithm) 就是解决问题的方法或者过程 数据结构(Data Structure) 是数 ...
- leetcode算题记录
数据库 查询第二高的薪资 题目描述 方法1:使用子查询找出最高的薪水,然后再找出小于该薪水的最大值就是第二高值. select max(distinct Salary) as SecondHighes ...
- 一个算法笨蛋的12月leetCode刷题日记
类似文章 一个算法笨蛋的2021年11月leetCode刷题日记 一个算法笨蛋的2021年12月leetCode刷题日记 一个算法笨蛋的2022年1月leetCode刷题日记 一个算法笨蛋的2022年 ...
- 【LeetCode】2022 7月 每日一题
[LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...
- # 异运算_一年级数学:3000道20内纯进、退位口算题,每天100道日新又月异
孩子上小学一年级就要正式接触数学,通过数学上学期的学习,基本上10以内的加减法可以解决了,但是还不够熟练.到了一年级数学下学期,就要解决20以内的加减法了.我们大人觉得这些都很简单,但是孩子不同,他们 ...
- LeetCode算法题-Factorial Trailing Zeroes(Java实现)
这是悦乐书的第183次更新,第185篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第42题(顺位题号是172).给定一个整数n,返回n!中的尾随零数.例如: 输入:3 输 ...
- LeetCode高频题:戈壁滩种树,一排n棵树,至少有k棵树存活时,最终形成的风景线有多少不同的情况
LeetCode高频题:戈壁滩种树,一排n棵树,至少有k棵树存活时,最终形成的风景线有多少不同的情况 提示:本题是系列LeetCode的150道高频题,你未来遇到的互联网大厂的笔试和面试考题,基本都是 ...
- 细节炸裂,字节官网首发Leetcode刷题手册被泄露,Github一天登顶
算法支持的语言 C, C++, Java, Python, C#, JavaScript, Ruby, Bash, MySQL 所以无论你是做Java开发的,又或者是 Python, C#等语言的,学 ...
最新文章
- 曝贾扬清第二跳,加入阿里!达摩院或将承载中国下一个AI愿景?
- 从oracle9i/92数据库中导出数据至 oracle 8.1.7 数据库中
- 蓝桥杯入门 (二)
- MySQL - mysqldump多种方式实现数据迁移
- 实战:使用IPSec保护服务器安全
- mysql 存储过程代码_pymysql存储过程代码
- java redis 面试题_Java开发人员怎么面试 常见Redis面试题有哪些
- Cannot forward after response has been committed
- 【OCR技术】字符识别技术总览
- 【转】运用jieba库分词
- html百分比实现边框而不挤出
- 读书札记:Fiddler--中文版(本人自己汉化的)下载
- 发票验证出现服务器证书出错,网上认证发票平台证书密码出现错误怎么办?
- 利用vegas去除视频水印
- 面试被问到【未来3-5年的职业规划】,到底该怎么回答?
- 域名被劫持应该如何处理
- 512内存安装php7,《滴水石穿-php》虚拟机中安装php7内存错误
- 汽车4G车载TBOX智能信息终端
- Coding and Paper Letter(五十三)
- 在网页中加入MSN、QQ以实现即时通讯