摘要: 思路: 题意为:只要有一个元素出现次数>=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 人间烟火地三鲜 阅读(118) 评论(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 人间烟火地三鲜 阅读(182) 评论(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 人间烟火地三鲜 阅读(130) 评论(0) 推荐(0)
摘要: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 在杨辉三角中,每个数是它左上方和右上方的数的和。 1 class Solution(object): 2 def generate(self, numRows): 3 """ 4 :type numRows: int 5 :r 阅读全文
posted @ 2020-04-21 23:06 人间烟火地三鲜 阅读(140) 评论(0) 推荐(0)
摘要: 代码一: 1 class Solution(object): 2 def removeElement(self, nums, val): 3 """ 4 :type nums: List[int] 5 :type val: int 6 :rtype: int 7 """ 8 if nums == [ 阅读全文
posted @ 2020-04-21 23:02 人间烟火地三鲜 阅读(79) 评论(0) 推荐(0)
摘要: 1 class Solution(object): 2 def removeDuplicates(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 i = 0 8 while i < len(nums) - 1: 9 i 阅读全文
posted @ 2020-04-21 22:58 人间烟火地三鲜 阅读(154) 评论(0) 推荐(0)
摘要: 通常可以写出的二分法:mid = (start + end) / 2; 另一种写法:mid = start + (end - start) / 2,可防止 (start + end) 溢出。 代码一: 1 class Solution(object): 2 def search(self, nums 阅读全文
posted @ 2020-04-21 22:54 人间烟火地三鲜 阅读(186) 评论(0) 推荐(0)
摘要: 1 # The guess API is already defined for you. 2 # @param num, your guess 3 # @return -1 if my number is lower, 1 if my number is higher, otherwise ret 阅读全文
posted @ 2020-04-21 22:49 人间烟火地三鲜 阅读(173) 评论(0) 推荐(0)
摘要: 1 # The isBadVersion API is already defined for you. 2 # @param version, an integer 3 # @return a bool 4 # def isBadVersion(version): 5 6 class Soluti 阅读全文
posted @ 2020-04-21 22:48 人间烟火地三鲜 阅读(115) 评论(0) 推荐(0)
摘要: 思路:双指针。 1 class Solution(object): 2 def twoSum(self, numbers, target): 3 """ 4 :type numbers: List[int] 5 :type target: int 6 :rtype: List[int] 7 """ 阅读全文
posted @ 2020-04-21 22:46 人间烟火地三鲜 阅读(129) 评论(0) 推荐(0)
摘要: 思路: 用二分查找,端点值从0和x开始,当两端点数相邻了停止循环。若停止循环了,则返回左端点——较小者。 1 class Solution(object): 2 def mySqrt(self, x): 3 """ 4 :type x: int 5 :rtype: int 6 """ 7 if x 阅读全文
posted @ 2020-04-21 22:43 人间烟火地三鲜 阅读(234) 评论(0) 推荐(0)
摘要: list[] 和 list[:] 的理解 list“赋值”时会用到list2 = list1 或者 list2[:] = list1,前者两个名字指向同一个对象,后者两个名字指向不同对象。理解如下: 首先,python中没有赋值的说法,只有名称到对象的引用; list2 = list1是把list1 阅读全文
posted @ 2020-04-21 22:35 人间烟火地三鲜 阅读(3444) 评论(0) 推荐(0)