摘要:
思路: 想到:绝对值应是非负数,所以k<0的情况下要排除(测试用例有k<0的);设置计数器ans=0;1、转set去重,再转回list,将newnums[]升序排列;2、若k==0,则 2-diff 数对是相同元素对,所以遍历newnums,其中元素nums.count(newnums[i]) >= 阅读全文
posted @ 2020-04-22 23:19
人间烟火地三鲜
阅读(157)
评论(0)
推荐(0)
摘要:
非递归实现。 1 class Solution(object): 2 def fib(self, N): 3 """ 4 :type N: int 5 :rtype: int 6 """ 7 fibs = [] 8 fibs.append(0) 9 fibs.append(1) 10 if N <= 阅读全文
posted @ 2020-04-22 23:16
人间烟火地三鲜
阅读(139)
评论(0)
推荐(0)
摘要:
思路: 1、处理好len(nums) < 2时的情形;2、用指针i遍历nums,用ans[]存放每一串连续1的长度,用计数器count记录: i指向的是1则计数器加1; i指向的不是1且前一位是1,则将计数器值添加到ans[]中,并清空计数器;3、返回max(ans)。 1 class Soluti 阅读全文
posted @ 2020-04-22 23:14
人间烟火地三鲜
阅读(210)
评论(0)
推荐(0)
摘要:
sorted()函数返回值:排好序的list——set经sorted()后也返回list。 sorted()和sort()理解与区别详见:https://www.cnblogs.com/panweiwei/p/12712756.html 代码一: 1 class Solution(object): 阅读全文
posted @ 2020-04-22 23:13
人间烟火地三鲜
阅读(142)
评论(0)
推荐(0)
摘要:
两中写法都过了。 学到的是sorted()函数排其他可迭代对象后返回值是list类型。 代码一: 1 class Solution(object): 2 def thirdMax(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 " 阅读全文
posted @ 2020-04-22 23:10
人间烟火地三鲜
阅读(166)
评论(0)
推荐(0)
摘要:
思路: 双指针(快慢指针)。 代码一: 1 class Solution(object): 2 def moveZeroes(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: None Do not return anything, modif 阅读全文
posted @ 2020-04-22 23:05
人间烟火地三鲜
阅读(117)
评论(0)
推荐(0)
摘要:
1 class Solution(object): 2 def missingNumber(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 for i in range(len(nums) + 1): 8 if i n 阅读全文
posted @ 2020-04-22 23:02
人间烟火地三鲜
阅读(161)
评论(0)
推荐(0)
摘要:
前两个超时,第三个用的set过了。 代码一: 1 class Solution(object): 2 def containsNearbyDuplicate(self, nums, k): 3 """ 4 :type nums: List[int] 5 :type k: int 6 :rtype: 阅读全文
posted @ 2020-04-22 23:01
人间烟火地三鲜
阅读(155)
评论(0)
推荐(0)
摘要:
思路: 1、从左往右遍历,每一趟找出此前的最大值max,若nums[i]<max,则num[i]是需要参与重排的;2、遍历到最右端,记录下最右边一个需要重排元素,其下标为high;3、从右往左遍历,每一趟找出此前的最小值min,若nums[i]>min,则num[i]是需要参与重排的;4、遍历到最左 阅读全文
posted @ 2020-04-22 22:48
人间烟火地三鲜
阅读(197)
评论(0)
推荐(0)

浙公网安备 33010602011771号