借鉴(leetcode_previous)
小小布丁--pudding
一只爬行的小乌龟~~
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode_1_Array_289生命游戏
这题是自己写出来的,思路比较简单,就是比较耗内存。思想就是为了简化边界上的数字,而给它四周填满0.执行用时 :12 ms, 在所有Python提交中击败了100.00%的用户内存消耗 :11.7 MB, 在所有Python提交中击败了32.43%的用户Time Complexity:O(2n)Space Complexity:O(2n)Code:class Solu...原创 2019-08-09 09:53:54 · 160 阅读 · 0 评论 -
Leetcode_1_Array_42_接雨水
关键:每根柱子的出水量=min(它自己左边最高柱子,它自己右边最高柱子) - 它自己的高度; 双指针;Time Complexity: O(n) ,因为扫描了n个元素Space Complexity:O(1), 因为只有三个永久变量class Solution(object): def trap(self, height): ...原创 2019-07-11 22:31:22 · 112 阅读 · 0 评论 -
Leetcode_1_Array_11_盛水最多的容器
1.自己挣扎的死笨版,然而严重超时了。以上版本完全可以选用动态替换,只需一个变量max_area.双向指针法,好处:时间复杂度O(n),因为扫描一次;空间复杂度O(1),因为仅一个恒定变量max_area。Code:class Solution { public int maxArea(int[] height) { int max_area=0,l=0,...原创 2019-07-11 21:34:51 · 114 阅读 · 0 评论 -
Leetcode_1_Array_309_Best Time to Buy and Sell Stock with Cooldown
Finally~我简直太爱刷力扣了,现在~~~!!!interesting!执行用时 :2 ms, 在所有Java提交中击败了98.05%的用户内存消耗 :34.9 MB, 在所有Java提交中击败了84.74%的用户Code:class Solution { public int maxProfit(int[] prices) { in...原创 2019-07-10 12:34:53 · 100 阅读 · 0 评论 -
Leetcode_1_Array_188_Best Time to Buy and Sell Stock IV
经过不断地调试,敲出来了。利用动态编程的思想,DP;类似于组合数学上的动态递归;idea的灵感来源于:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/solution/yi-ge-tong-yong-fang-fa-tuan-mie-6-dao-gu-piao-w-5/Code:cl...原创 2019-07-10 11:44:21 · 109 阅读 · 0 评论 -
Leetcode_1_Array_123_Best Time to Buy and Sell Stock III
开发出来一个超时版本。从中吸取的教训就是不要轻易使用递归,节约时间成本的意识要加强。Code:class Solution(object): def maxProfit_1(self, prices): """ :type prices: List[int] :rtype: int """ m...原创 2019-04-19 15:28:31 · 224 阅读 · 0 评论 -
Leetcode_1_Array_122_Best Time to Buy and Sell Stock II
这题我自己实在没想出来,看到一位大佬的代码,一句话就搞定。Key:把看似复杂的问题简化真的是太重要了,重点是要发现其中的规律,怎么给它形象化。 道理其实很简单:买涨不买跌,这用到了贪心算法的思想,就是只要有利可图,就立马买,不等时间,时间不等人~~~Code:class Solution(object): def maxProfit(self, prices):...原创 2019-04-18 17:13:45 · 159 阅读 · 0 评论 -
Leetcode_1_Array_121_Best Time to Buy and Sell Stock
好气哦,我这方法总是会有一个实例通过不了,就是特别长的那个实例,paste如下,欢迎大家找到错误并在留言评论~~~My wrong code:class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int ...原创 2019-04-17 22:52:32 · 134 阅读 · 0 评论 -
Leetcode_1_Array_45_Jump Game II
讲真,这道标榜为hard的题,我一下子捣鼓出来了还真有点儿受宠若惊。怎么说呢,经验就是,下键盘前一定要分析找到一条十分清晰的思路,良好的思路是成功的一半,加油~~~My code:class Solution(object): def jump(self, nums): """ :type nums: List[int] :rtype...原创 2019-04-17 21:41:24 · 138 阅读 · 0 评论 -
Leetcode_1_Array_55_Jump Game
这道题耗费了我特别多特别多的时间。。。。。还没有弄出来,只是弄出来一个超时版本,搬出来纪念一下:Code:class Solution(object): def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ le=len(...原创 2019-04-17 15:38:12 · 151 阅读 · 0 评论 -
Leetcode_1_Array_220_Contains Duplicate III
花点时间Debug,还是捣鼓出来了,效果还行~~~My own code:class Solution(object): def containsNearbyAlmostDuplicate(self, nums, k, t): """ :type nums: List[int] :type k: int :type t...原创 2019-04-17 10:43:39 · 190 阅读 · 0 评论 -
Leetcode_1_Array_219_Contains Duplicate II
请叫我Debug小能手,捂脸~~~因为一次性写成功真的很难,会忽略各种特殊情况,所以必须不停Debug~~~My own code:class Solution(object): def containsNearbyDuplicate(self, nums, k): """ :type nums: List[int] :type k:...原创 2019-04-17 10:05:30 · 125 阅读 · 0 评论 -
Leetcode_1_Array_217_Contains Duplicate
这题肥肠简单,不多说了,直接上代码~Code:class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ if len(set(nums))!=len(nums...原创 2019-04-17 09:43:17 · 118 阅读 · 0 评论 -
Leetcode_1_Array_275_H-Index II
这题真没啥好讲的,相比上一题,只需要加一条注释即可。然而我在复制粘贴时还是报错的,我觉着是粘贴的格式原因,再粘贴一遍就okay了,欧耶~~·~Code:class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :r...原创 2019-04-16 23:03:41 · 123 阅读 · 0 评论 -
Leetcode_1_Array_274_H-Index
吃奶的劲都使出来了,不停地Debug,总算捣鼓出来了。这题的难点不在于思路,而在于各种特殊情况的考虑~~~Code:class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ ...原创 2019-04-16 22:47:44 · 126 阅读 · 0 评论 -
Leetcode_1_Array_334_递增的三元子序列
Key:此刻最左(smallest),此刻最右(biggest),然后时刻替代比较并它俩,若某时刻的值大于此刻最右(biggest),则该序列已找到。还有就是要注意特殊情况,下面的小于等于号不能改为小于号,否则出错。执行用时 :1 ms, 在所有Java提交中击败了99.72%的用户内存消耗 :39.1 MB, 在所有Java提交中击败了66.67%的用户Code:clas...原创 2019-07-12 09:25:54 · 104 阅读 · 0 评论 -
Leetcode_1_Array_164_最大间距
受宠若惊,5分钟搞定~~执行用时 :40 ms, 在所有Python提交中击败了100.00%的用户内存消耗 :12.4 MB, 在所有Python提交中击败了34.21%的用户class Solution(object): def maximumGap(self, nums): """ :type nums: List[int] ...原创 2019-07-13 11:33:12 · 113 阅读 · 0 评论 -
Leetcode_8_Binary Search_374猜数字大小
执行用时 :20 ms, 在所有Python提交中击败了82.66%的用户内存消耗 :11.7 MB, 在所有Python提交中击败了32.27%的用户Code:class Solution(object): def guessNumber(self, n): """ :type n: int :rtype: int ...原创 2019-08-15 09:15:29 · 118 阅读 · 0 评论 -
Leetcode_8_Binary Search_162寻找峰值
二分法的思想:需要一个左指针和一个右指针,思想就是如果nums[mid]<nums[mid+1],那么left=mid+1;否则,right=mid,这样一来,每次就能将搜索范围缩小一半,时间复杂度为O(log(n)).执行用时 :36 ms, 在所有Python提交中击败了96.88%的用户内存消耗 :11.9 MB, 在所有Python提交中击败了21.14%的用户C...原创 2019-08-15 09:07:53 · 115 阅读 · 0 评论 -
Leetcode_8_Binary Search_154寻找旋转排序数组中的最小值II
一行话版本:执行用时 :44 ms, 在所有Python提交中击败了96.31%的用户内存消耗 :11.9 MB, 在所有Python提交中击败了35.00%的用户class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtyp...原创 2019-08-14 22:57:13 · 106 阅读 · 0 评论 -
Leetcode_8_Binary Search_153寻找旋转排序数组中的最小值
1.自己写的天花乱坠的二分查找却总是出错,无奈之下写了一行代码就直接通过了,虐心。执行用时 :36 ms, 在所有Python提交中击败了74.86%的用户内存消耗 :11.9 MB, 在所有Python提交中击败了29.33%的用户Code:class Solution(object): def findMin(self, nums): """...原创 2019-08-14 22:35:30 · 125 阅读 · 0 评论 -
Leetcode_8_Binary Search_81搜索旋转排序数组II
1.方法有点投机取巧,然而时间空间效果上不是很好,主要是sorted函数的复杂度本身就是O(n)了。Code:执行用时 :84 ms, 在所有Python提交中击败了10.24%的用户内存消耗 :12 MB, 在所有Python提交中击败了23.03%的用户class Solution(object): def search(self, nums, target):...原创 2019-08-14 17:06:36 · 123 阅读 · 0 评论 -
Leetcode_8_Binary Search_33搜索旋转排序数组
由于该题的时间复杂度要求为Olog(n),故想到先要找到最小数所在索引,然后确定将问题定位在左升序区间还是右升序区间,代码写得稍稍有点乱。另外,封装函数法可大大减少运行时间。执行用时 :60 ms, 在所有Python提交中击败了7.95%的用户内存消耗 :12.1 MB, 在所有Python提交中击败了5.10%的用户Code:class Solution(object)...原创 2019-08-14 10:01:05 · 109 阅读 · 0 评论 -
Leetcode_8_Binary Search_35插入搜索位置
自己思路很清楚,但是就是while循环里的条件一定要加上=号,这样的话才能跳出循环。执行用时 :1 ms, 在所有Java提交中击败了86.14%的用户内存消耗 :38 MB, 在所有Java提交中击败了76.99%的用户Code:class Solution { public int searchInsert(int[] nums, int target) {...原创 2019-08-13 17:47:08 · 106 阅读 · 0 评论 -
Leetcode_8_Binary Search_278第一个错误的版本
该题是最基础的二分查找;思想就是:三个变量:left,mid,right;需注意的是,为了防止内存溢出,mid需定义为left+(right-left)/2;Code:public class Solution extends VersionControl { public int firstBadVersion(int n) { int left=0,rig...原创 2019-08-13 17:16:35 · 98 阅读 · 0 评论 -
Leetcode_1_Array_56合并区间
该题是57题的easy version,类似。执行用时 :92 ms, 在所有Python提交中击败了70.15%的用户内存消耗 :14 MB, 在所有Python提交中击败了100.00%的用户Code:class Solution(object): def merge(self, intervals): """ :type int...原创 2019-08-12 09:57:39 · 126 阅读 · 0 评论 -
Leetcode_1_Array_57插入区间
思路是:将当前的newInterval插入到intervals中去,然后进行sorted()排序,排完序后,遍历,将intervals[i+1][0]与intervals[i][1]进行比较。若intervals[i+1][0]<=interval[i][1],然后将intervals[i+1][1]替换为max(intervals[i+1][1],interval[i][1]),则将i+=...原创 2019-08-11 23:04:29 · 104 阅读 · 0 评论 -
Leetcode_1_Array_4寻找两个有序数组的中位数
这题很开心是自己做出来的。虽然一开始老搞不明白的出错,后来才发现是java除法计算的结果默认只取整数部分,所以要强转。 double result=(double)(num_all[len3/2-1]+num_all[len3/2])/2;这句是精髓。执行用时 :11 ms, 在所有Java提交中击败了88.72%的用户内存消耗 :53.3 MB, 在所有Java提交中击败了67....原创 2019-07-29 13:23:12 · 122 阅读 · 0 评论 -
Leetcode_1_Array_330按要求补齐数组
这一题不是特别好理解,用的是贪心算法,算是我的初识贪心算法,慢慢建立概念。执行用时 :2 ms, 在所有Java提交中击败了91.23%的用户内存消耗 :39.2 MB, 在所有Java提交中击败了30.77%的用户Code:class Solution { public int minPatches(int[] nums, int n) { int...原创 2019-07-29 11:52:54 · 224 阅读 · 0 评论 -
Leetcode_1_Array_135_分发糖果
这题我自己捣鼓了好久都有问题,最后在Solution里发现了一种特别好的算法:一个好的算法简直太重要了!借用两个数组,left_to_right[ ],right_to_left[ ];分三步走:1.只跟左边相邻的数比,若更大,则此处的糖果数比左边的多一;2.只跟右边相邻的数比,若更大,则此处的糖果数比左边的多一;3.sum糖果数加Math.max(left_to_righ...原创 2019-07-28 20:06:58 · 134 阅读 · 0 评论 -
Leetcode_1_Array_287_寻找重复数
两个算法思想精髓;1.对于数组而言,如果是要遍历作比较,完全可以先排序再比较,因为这样只需要跟其前一个或后一个数作比较;2.Set思想,一次加一个元素,每次加入之前判断一下,如果某元素已存在Set中,则说明其是重复元素。Code version one:执行用时 :7 ms, 在所有Java提交中击败了45.50%的用户内存消耗 :39.5 MB, 在所有Java提交中...原创 2019-07-25 14:54:17 · 167 阅读 · 0 评论 -
Leetcode_1_Array_229_Majority Element II
跟119题类似,稍微捣鼓一下就OK~~~Code:class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: List[int] """ unique=set(nums) ...原创 2019-04-16 14:51:52 · 133 阅读 · 0 评论 -
Leetcode_1_Array_169_Majority Element
又自己捣鼓出来一题,开心啊啊啊!!!~~~Code:class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ unique=set(nums) le...原创 2019-04-16 14:45:28 · 134 阅读 · 0 评论 -
Leetcode_1_Array_53最大子序和
这题需要两个变量:此前的最优值res及截至目前的可能最优值sum.执行用时 :92 ms, 在所有Python3提交中击败了50.72%的用户内存消耗 :14.4 MB, 在所有Python3提交中击败了5.04%的用户Code:class Solution: def maxSubArray(self, nums: List[int]) -> int: ...原创 2019-08-22 12:21:25 · 110 阅读 · 0 评论 -
Leetcode_8_Binary Search_315计算右侧小于当前元素的个数
1.动态编程:DP好处:建立一个动态数组,代表到当前为止最好的结果,动态地找出最好结果之间的关系。非常灵活,值得反复咀嚼,搞透为止。执行用时 :1260 ms, 在所有Python提交中击败了12.89%的用户内存消耗 :12.1 MB, 在所有Python提交中击败了5.28%的用户Code:class Solution: def lengthOfLIS(s...原创 2019-08-17 11:52:04 · 113 阅读 · 0 评论 -
Leetcode_2_String_242有效的字母异位词
主要是对异位词这个概念的理解,就是说字母相同,只是顺序有变。执行用时 :96 ms, 在所有Python3提交中击败了34.92%的用户内存消耗 :14.6 MB, 在所有Python3提交中击败了5.17%的用户Code:class Solution: def isAnagram(self, s: str, t: str) -> bool: i...原创 2019-08-26 13:25:11 · 104 阅读 · 0 评论 -
Leetcode_2_String_205同构字符串
又学会了Python String这一块一条新的语法:list(map(s.index,s))其中,s.index即可得到字符串中每个字符首次出现的索引位置,以数组的形式呈现。map(function,iterable,...):对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回执行用时 :72 ms, 在所有Python3提交中...原创 2019-08-25 23:13:14 · 112 阅读 · 0 评论 -
Leetcode_2_String_345反转字符串里的元音字母
这题有几处关键的地方:1.先用list(s)函数将字符串转换成字符串数组2.元音字母集合大小写都要收纳进去3.最后的结果要用"".join()拼接。执行用时 :88 ms, 在所有Python3提交中击败了59.82%的用户内存消耗 :14.6 MB, 在所有Python3提交中击败了18.57%的用户Code:class Solution: def r...原创 2019-08-25 20:40:42 · 115 阅读 · 0 评论 -
Leetcode_2_String_151翻转字符串里的单词
这题主要用到s.strip()函数,在默认情况下,它会默认为将空格、制表符等作为分隔符。另外,数组的[::-1]函数实现反转也是很强哦~还要注意不能直接写为returns.split()[::-1],不然结果就成了所以一定要有" ".join()函数去拼接。执行用时 :40 ms, 在所有Python3提交中击败了96.91%的用户内存消耗 :14.3 MB, 在所有...原创 2019-08-25 20:19:25 · 122 阅读 · 0 评论 -
Leetcode_2_String_383赎金信
思路非常巧妙,用到了Python的replace(old,new,maxTimes)函数,但要注意该函数并不会原地变化,故需要一步赋值操作。、执行用时 :52 ms, 在所有Python3提交中击败了96.90%的用户内存消耗 :13.9 MB, 在所有Python3提交中击败了5.17%的用户Code:class Solution(object): def can...原创 2019-08-25 20:01:07 · 142 阅读 · 0 评论
分享