随笔分类 -  Python

上一页 1 ··· 6 7 8 9 10 11 12 13 下一页
摘要:给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 在杨辉三角中,每个数是它左上方和右上方的数的和。 1 class Solution(object): 2 def generate(self, numRows): 3 """ 4 :type numRows: int 5 :r 阅读全文
posted @ 2020-04-21 23:06 人间烟火地三鲜 阅读(138) 评论(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 人间烟火地三鲜 阅读(77) 评论(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 人间烟火地三鲜 阅读(153) 评论(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 人间烟火地三鲜 阅读(184) 评论(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 人间烟火地三鲜 阅读(172) 评论(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 人间烟火地三鲜 阅读(128) 评论(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 人间烟火地三鲜 阅读(233) 评论(0) 推荐(0)
摘要:list[] 和 list[:] 的理解 list“赋值”时会用到list2 = list1 或者 list2[:] = list1,前者两个名字指向同一个对象,后者两个名字指向不同对象。理解如下: 首先,python中没有赋值的说法,只有名称到对象的引用; list2 = list1是把list1 阅读全文
posted @ 2020-04-21 22:35 人间烟火地三鲜 阅读(3436) 评论(0) 推荐(0)
摘要:思路: 声明矩阵list; 返回按曼哈顿距离排序的list; 1 class Solution(object): 2 3 def allCellsDistOrder2(self, R, C, r0, c0): 4 """ 5 :type R: int 6 :type C: int 7 :type r 阅读全文
posted @ 2020-04-17 23:19 人间烟火地三鲜 阅读(282) 评论(0) 推荐(0)
摘要:思路:1、设置res=[],extra[];2、遍历arr2,统计arr1中arr2[i]的个数num;3、在res中追加num个arr2[i];4、遍历arr1,将没出现在arr2中的元素添加到extra中;5、将extra升序排列,返回res+extra。 1 class Solution(ob 阅读全文
posted @ 2020-04-17 23:15 人间烟火地三鲜 阅读(220) 评论(0) 推荐(0)
摘要:思路:1、降序排列nums;2、不断从nums中取出最大元素追加到res中,直到res元素的和大于nums;3、返回res。 1 class Solution(object): 2 def minSubsequence(self, nums): 3 """ 4 :type nums: List[in 阅读全文
posted @ 2020-04-17 23:12 人间烟火地三鲜 阅读(210) 评论(0) 推荐(0)
摘要:方法一:思路见注释。 1 class Solution(object): 2 def merge(self, intervals): 3 """ 4 :type intervals: List[List[int]] 5 :rtype: List[List[int]] 6 """ 7 if len(i 阅读全文
posted @ 2020-04-17 23:07 人间烟火地三鲜 阅读(211) 评论(0) 推荐(0)
摘要:思路: 1、将prices升序排列;2、用price遍历排序后的prices,取price在原list中的下标i;3、在原list中截取下标i之后的元素,取其中最大值max(temp[i:]);4、计算当前利润:max(temp[i:]) - price,存入ans[]中;5、返回ans中的最大值。 阅读全文
posted @ 2020-04-17 23:01 人间烟火地三鲜 阅读(198) 评论(0) 推荐(0)
摘要:思路: 指针i和j分别遍历nums1和nums2; 取两指针较小者追加到res中,较小指针后移,较大者不动; 若两指针相等,则两者都追加到res中,两指针均后移; i<m或j<n时,停止遍历,将两串之一剩余的部分有序序列追加到res中。注:本题提交的时候,程序不用return,另外本题必须在nums 阅读全文
posted @ 2020-04-17 22:59 人间烟火地三鲜 阅读(250) 评论(0) 推荐(0)
摘要:思路: 将原list中的数字拼接成字符串res;将int(res) + 1转成list。 1 class Solution(object): 2 def plusOne(self, digits): 3 """ 4 :type digits: List[int] 5 :rtype: List[int 阅读全文
posted @ 2020-04-17 22:53 人间烟火地三鲜 阅读(159) 评论(0) 推荐(0)
摘要:思路:折半查找,关键在于停止循环的条件。 1 class Solution(object): 2 def searchInsert(self, nums, target): 3 """ 4 :type nums: List[int] 5 :type target: int 6 :rtype: int 阅读全文
posted @ 2020-04-17 22:49 人间烟火地三鲜 阅读(201) 评论(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 len(num 阅读全文
posted @ 2020-04-17 22:47 人间烟火地三鲜 阅读(133) 评论(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 if 阅读全文
posted @ 2020-04-17 22:43 人间烟火地三鲜 阅读(125) 评论(0) 推荐(0)
摘要:思路:即先拼接string的升序序列,再拼接其降序序列。1、将string转成list;2、升序排list,各元素取一个拼接到res,将拼接过的元素从原list中删除;3、降序排list,各元素取一个拼接到res,将拼接过的元素从原list中删除;4、重复2和3,直到list为空。若最小或者最大字符 阅读全文
posted @ 2020-04-16 14:47 人间烟火地三鲜 阅读(275) 评论(0) 推荐(0)

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