自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(17)
  • 收藏
  • 关注

原创 LeetCode340:至多包含 K 个不同字符的最长子串(python)

题解:双指针 滑动窗口import collectionsclass Solution(object): def lengthOfLongestSubstringKDistinct(self,s:str,k:int)->int: ans =0 left = right = 0 size = len(s) count = collections.Counter() while right<size:

2022-01-26 10:46:37 1291

原创 LeetCode524:通过删除字母匹配到字典里最长单词(python)

题解:s 中每个字符和 dictionary 中每个 字符串 进行比较,记录最长的那一个,且字典序是最小的。先排序,解决最长字符串的同时字典序最小的问题后比较,两个指针,分别指向 s 和 dictionary 中的字符串,挨个比较。当字符串的指针长度跟字符串本身长度一致,就说明 s 删除一些子串可以变成字符串class Solution: def findLongestWord(self, s: str, dictionary: List[str]) -> str: ..

2022-01-25 20:52:32 593

原创 LeetCode680:Valid Palindrome 验证回文字符

题解:定义一个判断回文的函数,然后递归,也是用左右两个指针,同167 和 633class Solution(object): def validPalindrome(self, s: str) -> bool: def checkPalindrome(i, j): while i < j: if s[i]!= s[j]:return False i+=1 .

2022-01-25 19:40:03 303

原创 LeetCode633.Sum of Square Number(Python)

167的变形题,也是用两个指针题解:class Solution: def judgeSquareSum(self, c: int) -> bool: a = 0 b = int(c ** 0.5) while a <= b: if a ** 2 + b ** 2 == c: return True elif a ** 2 + b ** 2 >

2022-01-25 19:15:39 778

原创 LeetCode76:滑动窗口 Minimum Window Substring(python)

题解:滑动窗口import collectionsclass Solution(object): def minWindow(self,s:str,t:str)->str:############分析过程####################### #Sliding Window + 2 pointers # S= 'A (D O B E (C (O D E B A) N C)';T='ABC' # left

2022-01-25 19:13:18 275

原创 leetCode142:(python)

解答:class Solution: def detectCycle(self, head): if head is None or head.next is None or head.next.next is None: return None fast = head slow = head while fast and fast.next: fast = fast.next.ne

2022-01-23 20:24:15 468

原创 LeetCode88:合并排序(python)

合并后直接排序class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ nums1[m:]=nums2 nums1.sort() 方法2:...

2022-01-21 10:34:56 904

原创 LeetCode167:Two Sum(python)

解1:执行代码时时间超出限制class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: for i in range(len(numbers)): for j in range(i+1,len(numbers)): # if i < j: if numbers[i]+num

2022-01-20 20:39:30 398

原创 LeetCode665:非递减序列(python)

题目:一个长度为 n 的整数数组,在最多改变1个元素的情况下,该数组能否变成一个非递减数列: 对于数组中任意的 i (0 <= i <= n-2),总满足 nums[i] <= nums[i + 1]。思路:非递减数列,也就是后面的数要大于等于前面,有一个则计数加一所以:count=0,天然是非递减数列,返回turecount>1,返回False对于count=0的情况,要分类讨论(也主要卡在这里开始写的时候)情况一:[5,4,6,7]第一个数大于第二个,

2022-01-15 19:31:43 782

原创 LeetCode406:重排队列(python)

题目:当前是数组是打乱顺序的people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)思路:贪心算法;这里有两个指标,一个是身高,一个是前面有几个人,这两个不能同时排,得先顾一个,不然没个章法,不知道以什么为标准

2022-01-14 20:38:22 577

原创 Leetcode122:抛售股票最大利润(python)

题目:给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)思路:贪心策略:后一天的股票价格比前一天高,就买入,利润加上两天价格之差class Solution(object): def maxprofit(self,prices): n = len(prices) s

2022-01-14 19:53:10 515

原创 Leetcode763:划分字母区间(python)

思路:先创建一个列表,存放26个字母,起始a-z每个字母的位置都是0 遍历字符串,记录每个字母的最远位置,这一步需要循环,代码如下: for i ,ch in enumerate(s): endList[ord(ch)-ord('a')]=i#例如对于字符串‘aba’i = 0,ch = ‘a’,endList[ord(ch)-ord('a')]=endList[0]=0=i,所以endList列表第一个位置也就是a的位置是0;i = 1,ch =

2022-01-13 08:36:36 464

原创 leetcode452_用最少的箭射气球(python)

思路:也是区间问题,与435相似,首先排序,以第一个区间的右边界作为对比,如果下一个区间的左边界比它小,则说明用一支箭可以射中两个区间,否则,两个区间是不重叠的,需要两支箭class Solution(object): def findMinArrowShots(self, points): if not points: return 0 nums = sorted(points, key=lambda x: x[1]) # 按照每

2022-01-12 22:02:00 273

原创 leetcode605:种花(python)

思路:#统计可以种花的数量,这个数大于想要种的数量n则返回True,否则返回Flase#把列表两端补充0,解决边界问题(这个思路很好,借鉴了别人的,可以省去分类讨论的情况)class Solution(object): def canPlaceFlowers(self, flowerbed,n): m = len(flowerbed) flowerbed = [0]+flowerbed+[0] m = len(flowerbed) .

2022-01-12 21:06:27 334

原创 Leetcode435:删除重叠区间(python)

贪心策略:优先保留结尾小且不相交的区间实现方法:先对区间结尾进行从小到大的排序,确保后一个区间的首大于前一个区间的尾,保留,计数加一class Solution(object): def eraseOverLapIntervals(self,intervals): if not intervals: return 0 nums = sorted(intervals,key=lambda x:x[1])#按照每一个区间的末尾进行排序

2022-01-11 22:29:01 966

原创 Leetcode455:分饼干(python)

class Solution(object): def findContentChildren(self,g,s): g.sort()#胃口 s.sort()#尺寸 child = 0 cookies = 0 while(child<len(g)) and (cookies<len(s)): if g[child]<= s[cookies]: c.

2022-01-11 21:44:59 531

原创 leetcode135:candy(python)

先从左到右遍历一遍,然后反过来class Solution(object): def candy(self,ratings): n = len(ratings) dp = [1 for i in range(n)] sum = 0 for i in range(n): if ratings[i]>ratings[i-1]: dp[i] = dp[i-1]+1

2022-01-11 20:54:27 249

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除