上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 51 下一页
摘要: 思路:折半查找,关键在于停止循环的条件。 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 人间烟火地三鲜 阅读(135) 评论(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 人间烟火地三鲜 阅读(127) 评论(0) 推荐(0)
摘要: 思路:即先拼接string的升序序列,再拼接其降序序列。1、将string转成list;2、升序排list,各元素取一个拼接到res,将拼接过的元素从原list中删除;3、降序排list,各元素取一个拼接到res,将拼接过的元素从原list中删除;4、重复2和3,直到list为空。若最小或者最大字符 阅读全文
posted @ 2020-04-16 14:47 人间烟火地三鲜 阅读(277) 评论(0) 推荐(0)
摘要: sort()函数: sort()函数是原址排序,字符串、元组没有sort()方法——因为均不可修改。 如果需要一个排序好的副本,同时保持原有列表不变,可以这样操作: 1 x =[4, 6, 2, 1, 7, 9] 2 3 y = x[ : ] 4 5 y.sort() 注:y = x[ : ] 通过 阅读全文
posted @ 2020-04-16 14:37 人间烟火地三鲜 阅读(553) 评论(0) 推荐(0)
摘要: 思路:想到:面积不为0,即能组成三角形,面积就不会是0,即满足两边之和大于第三边。1、将数组A降序排列,三个指针:up,mid,low分别指向前三个元素; 循环结束条件是:low=len(A);2、若满足两边之和大于第三边,即up<mid+low,则返回up+mid+low;3、不满足up<mid+ 阅读全文
posted @ 2020-04-16 14:29 人间烟火地三鲜 阅读(284) 评论(0) 推荐(0)
摘要: 思路:1、遍历A,取出其中的奇数(list1)、偶数(list2);2、下标为奇数时从list1中取一个元素插入;3、下标为偶数时从list2中取一个元素插入; 1 class Solution(object): 2 def sortArrayByParityII(self, A): 3 """ 4 阅读全文
posted @ 2020-04-16 14:27 人间烟火地三鲜 阅读(202) 评论(0) 推荐(0)
摘要: 思路详见注释。 1 class Solution(object): 2 def intersect(self, nums1, nums2): 3 """ 4 :type nums1: List[int] 5 :type nums2: List[int] 6 :rtype: List[int] 7 " 阅读全文
posted @ 2020-04-16 14:24 人间烟火地三鲜 阅读(165) 评论(0) 推荐(0)
摘要: 1 class Solution(object): 2 def intersection(self, nums1, nums2): 3 """ 4 :type nums1: List[int] 5 :type nums2: List[int] 6 :rtype: List[int] 7 """ 8 阅读全文
posted @ 2020-04-14 14:44 人间烟火地三鲜 阅读(226) 评论(0) 推荐(0)
摘要: 思路:1、s和t长度不同,返回false;2、转list,sort(),s==t则返回true,否则返回false。 1 class Solution(object): 2 def isAnagram(self, s, t): 3 """ 4 :type s: str 5 :type t: str 阅读全文
posted @ 2020-04-14 14:43 人间烟火地三鲜 阅读(180) 评论(0) 推荐(0)
上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 51 下一页