随笔分类 -  Python

上一页 1 ··· 5 6 7 8 9 10 11 12 13 下一页
摘要:代码一思路:遍历nums,每一趟遍历k个元素求和s前,将s清零,不过酱超时。 1 class Solution(object): 2 def findMaxAverage(self, nums, k): 3 """ 4 :type nums: List[int] 5 :type k: int 6 : 阅读全文
posted @ 2020-04-29 09:02 人间烟火地三鲜 阅读(179) 评论(0) 推荐(0)
摘要:代码一: 1 class Solution(object): 2 def maximumProduct(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 # 降序排列 8 nums.sort(reverse=True) 阅读全文
posted @ 2020-04-29 08:59 人间烟火地三鲜 阅读(170) 评论(0) 推荐(0)
摘要:代码一: 1 class Solution(object): 2 def canPlaceFlowers(self, flowerbed, n): 3 """ 4 :type flowerbed: List[int] 5 :type n: int 6 :rtype: bool 7 """ 8 if 阅读全文
posted @ 2020-04-29 08:56 人间烟火地三鲜 阅读(134) 评论(0) 推荐(0)
摘要:思路:将原nums中的元素都读出来,在往新list中加。 1 class Solution(object): 2 def matrixReshape(self, nums, r, c): 3 """ 4 :type nums: List[List[int]] 5 :type r: int 6 :ty 阅读全文
posted @ 2020-04-29 08:52 人间烟火地三鲜 阅读(138) 评论(0) 推荐(0)
摘要:1 class Solution(object): 2 def arrayPairSum(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 nums.sort() 8 ans = 0 9 for i in range(0 阅读全文
posted @ 2020-04-29 08:48 人间烟火地三鲜 阅读(123) 评论(0) 推荐(0)
摘要:1. x in s: 返回true:如果列表s中有元素x; 返回false:如果没有元素x; 2. x not in s 返回true:如果列表s中没有元素x; 返回false:如果有元素x; 3. s + t 将列表s和列表t连接起来; 4. s * n or n * s 将列表s重复n次; 5. 阅读全文
posted @ 2020-04-22 23:34 人间烟火地三鲜 阅读(576) 评论(0) 推荐(0)
摘要:思路: 想到:绝对值应是非负数,所以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 人间烟火地三鲜 阅读(155) 评论(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 人间烟火地三鲜 阅读(141) 评论(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 人间烟火地三鲜 阅读(116) 评论(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 人间烟火地三鲜 阅读(160) 评论(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 人间烟火地三鲜 阅读(195) 评论(0) 推荐(0)
摘要:思路: 题意为:只要有一个元素出现次数>=2,则返回true;否则返回false;利用list转set会去重的特点。需要注意:1、len(setnums) == len(nums)时,说明每个元素都是唯一的,返回false;2、只要去重后长度减小,说明有重复元素,则返回true。 代码一: 1 cl 阅读全文
posted @ 2020-04-21 23:27 人间烟火地三鲜 阅读(115) 评论(0) 推荐(0)
摘要:代码一: 1 class Solution(object): 2 def rotate(self, nums, k): 3 """ 4 :type nums: List[int] 5 :type k: int 6 :rtype: None Do not return anything, modify 阅读全文
posted @ 2020-04-21 23:24 人间烟火地三鲜 阅读(117) 评论(0) 推荐(0)
摘要:list、set、str的转换: str转list:list(str) list转set:set(list) set转list:list(set) 注:list转set时会自动去重! 将list[str] digits转成list[int]: 法一:[ int(i) for i in digits 阅读全文
posted @ 2020-04-21 23:18 人间烟火地三鲜 阅读(181) 评论(0) 推荐(0)
摘要:思路: 想到:prices中一列数字,任取一个为买入价格buy,在其右边任取一个为卖出价格sell;取[buy,...,sell]区间中相邻数字之差,这些差值求和为sum,则必有sell-buy = sum;本题中求最大收益,所以遍历prices,找到prices[i]-prices[i-1] > 阅读全文
posted @ 2020-04-21 23:13 人间烟火地三鲜 阅读(173) 评论(0) 推荐(0)
摘要:给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 在杨辉三角中,每个数是它左上方和右上方的数的和。 1 class Solution(object): 2 def getRow(self, rowIndex): 3 """ 4 :type rowIndex: int 5 :rty 阅读全文
posted @ 2020-04-21 23:08 人间烟火地三鲜 阅读(128) 评论(0) 推荐(0)

上一页 1 ··· 5 6 7 8 9 10 11 12 13 下一页