蜗牛选手王豆逗的刷题之路
文章平均质量分 56
wyxjohn205
蜗牛选手,青铜码农,来CSDN向各位王者学习。
展开
-
Binary Search_278_First Bad Version
Binary Search 278 First Bad Version Regular binary search question, provide python solution below# first version 32msclass Solution(object): def firstBadVersion(self, n): """ :type...原创 2018-03-25 16:11:21 · 99 阅读 · 0 评论 -
三种方法解决Lintcode39 Recover Rotated Sorted Array in Python
1. 三次翻转首先找到最小值的index,而后翻转三次。for example, [4,5,1,2,3], 先找到1的index2,再以2为轴:[4,5]->[5,4]; [1,2,3]->[3,2,1]; [5,4,3,2,1]->[1,2,3,4,5]class Solution: """ @param nums: An integer arra...原创 2018-09-26 07:29:12 · 216 阅读 · 0 评论 -
两种方法解决leetcode 53. Maximum Subarray
1. 第一种方法,pre_sum是无脑加,max是对比“pre_sum减去前面一个最小的元素”和current max的对比。把我自己绕进去了。上代码:def maxSubArray(self, nums): if len(nums)==0: return 0 pre_sum = 0 min_sum = 0 ...原创 2018-09-25 07:08:58 · 133 阅读 · 0 评论 -
两种方法解决leetcode206:Reverse Linked List
方法一:递归其实在正序递归的时候已经确定了tail元素。回溯的时候,进行两步1. 让head.next指向head2. 让head指向noneclass Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNo...原创 2018-09-14 09:31:57 · 156 阅读 · 0 评论 -
Python solving Topological Sorting with explanation
拓扑排序: class Solution: """ @param: graph: A list of Directed graph node @return: Any topological order for the given graph. """ def dfs(self,i,countrd,res): # dfs就是把入度为0的ite...原创 2018-06-20 11:23:15 · 145 阅读 · 0 评论 -
Leetcode78 Subset
构造递归函数。先加进数来找子集,再减去数来找其他子集。class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.results=[] ...原创 2018-06-02 05:26:05 · 154 阅读 · 0 评论 -
Partition方法(同向/双向双指针)解决 215. Kth Largest Element in an Array
1. quickselect. (相向双指针)。原理和partition里讲的一样。class Solution(object): def findKthLargest(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ ...原创 2018-05-31 03:51:26 · 521 阅读 · 0 评论 -
Lintcode 31. Partition Algorithm
Partition算法。快排实习的关键。 这里只需要把比k大的放右边,小的放左边,并没有继续再在左右partition里排序。相当于完成了快排的一个步骤。思想:双指针,左边和K对比,if nums[start]<k, 说明就不是一个swap candidate(因为比K小的本来就应该好好在左边呆着), 于是start+1继续往下找。同理,右边和K对比,if nums[end]>=k, ...原创 2018-05-30 06:48:10 · 261 阅读 · 0 评论 -
双指针解决Leetcode 15. 3 Sum
1. 3sum其实和2sum思路差不多。只要把第三个数变成2sum的target就好了。当然要先sort一下,原数组是无序的。2. 也要考虑重复数组的情况。比如left[i]==left[i-1]说明这个数字在i-1的时候应该考虑过了,需要skip.同理right也是一样。还有一种重复情况是,当target重复的时候,也要skip.3. 有个特殊情况我一直过不去。[0,0,0],差点用特殊情况if...原创 2018-05-30 04:07:14 · 220 阅读 · 0 评论 -
两种方法解决 Sort Color II - with multiple colors k
1. 最直观的方法,把list存成dictionary,key为color,value为color出现的次数,而后循环一个个导出。 def sortcolor2(colors,k): count={} for c in colors: if c not in count: count[c]=0 else: ...原创 2018-05-30 01:45:19 · 281 阅读 · 0 评论 -
两种方法解决 Leetcode167. Two Sum II - Input array is sorted
1. 双指针:class Solution: """ @param nums: an array of Integer @param target: target = nums[index1] + nums[index2] @return: [index1 + 1, index2 + 1] (index1 < index2) """ def t...原创 2018-05-27 00:56:28 · 87 阅读 · 0 评论 -
5. Longest Palindromic Substring
这题。。很迷。。。过一遍char,指针一左一右,找回文。一般是当left=right=某个数字的时候,进入while里找最长回文,同时updateleft and right. 其他情况abs(left-right)=1, 进入下一位。O(n*2)def longestPalindrome(s): ansl, ansr, maxx = 0, 1, 0 length ...原创 2018-05-19 05:33:23 · 76 阅读 · 0 评论 -
Leetcode_34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target i...原创 2018-03-27 10:39:02 · 81 阅读 · 0 评论 -
Leetcode_33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the a...原创 2018-03-27 10:31:42 · 96 阅读 · 0 评论 -
两种方法解决leetcode 153. Find Minimum in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exis...原创 2018-03-25 17:14:22 · 352 阅读 · 0 评论 -
三种方法解决Leetcode169. Majority Element in Python
1. dictionary class Solution: """ @param: nums: a list of integers @return: find a majority number """ def majorityNumber(self, nums): if len(nums)==0: ret...原创 2018-09-26 08:23:43 · 261 阅读 · 0 评论