LeetCode高频100【1】

目录

【1】

# Title Acceptance Difficulty
20 Valid Parentheses 35.7% Easy
21 Merge Two Sorted Lists 45.5% Easy
22 Generate Parentheses 52.8% Medium
23 Merge k Sorted Lists 32.6% Hard
31 Next Permutation 30.0% Medium
32 Longest Valid Parentheses 24.8% Hard
33 Search in Rotated Sorted Array 32.6% Medium
34 Find First and Last Position of Element in Sorted Array 32.8% Medium
39 Combination Sum 46.3% Medium
42 Trapping Rain Water 41.5% Hard

20. Valid Parentheses(Easy)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1)Open brackets must be closed by the same type of brackets.
2)Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example:
Input: "([)]" Output: false
Input: "{[]}" Output: true

# my solution ,Runtime: 32 ms, faster than 99.51%, T(n)=S(n)=O(n)
class Solution:
    def isValid(self, s: str) -> bool:
        if not s:
            return True
        stack=[]
        mapping = {')' : '(', '}' : '{', ']' : '['}
        for i in s:
            if i in "([{":
                stack.append(i)
            elif stack ==[]:
                return False
            else:
                temp=stack.pop(-1)
                if temp==mapping[i]:
                    continue
                else:
                    stack.append(temp)
                    stack.append(i)
        if stack:
            return False
        return True

21. Merge Two Sorted Lists(Easy)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# my solution, T(n)=S(n)=O(m+n)
# Runtime: 40 ms, faster than 99.28%
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        head=l3=ListNode(0)
        while l1 and l2:
            if l1.val< l2.val:
                l3.next = l1
                l1 = l1.next
            else:
                l3.next = l2
                l2 = l2.next
            l3 = l3.next
        res = l1 or l2
        while res:
            l3.next = res
            l3 = l3.next
            res = res.next
        return head.next

22. Generate Parentheses(Medium)

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

回溯法

# Approach 2: Backtracking
class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        ans=[]
        def backtrack(s='',l=0,r=0):
            if len(s)==2*n:
                ans.append(s)
            if l<n:
                backtrack(s+'(',l+1,r)
            if r<l:
                backtrack(s+')',l,r+1)
        backtrack()
        return ans

23. Merge k Sorted Lists(Hard)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# Approach 1: Brute Force, T(n)=O(NlogN) (sorted time), S(n)=O(N) 
class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        nodes=[]
        for l in lists:
            while l:
                nodes.append(l.val)
                l=l.next
        head = p = ListNode(0)
        for node in sorted(nodes):
            p.next = ListNode(node)
            p = p.next
        return head.next

# Approach 2: Compare one by one
# Approach 3: Optimize Approach 2 by Priority Queue, use in python2
# T(n)=O(Nlogk) (sorted time), S(n)=O(N) 
"""
 Problem of python3:
 为了获取到当前node.val最小对应到的那个ListNode对象。
 理论上tuple第二个位置的ListNode对象的大小关系是不被关心的(我们关心的起作用的应该是tuple第一个位置存放的node.val),
 但实际上,当第一个位置的node.val相同的时候,会继续比较tuple的第二个位置的大小关系,
 这时因为python3不支持未定义__lt__方法的ListNode对象之间的大小比较,所以就发生了TypeError: '<' not supported between instances of 'ListNode' and 'ListNode'
(可以看到发生Runtime Error的那组样例存在部分的value相同的node)。
"""
from queue import PriorityQueue
class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        head = p = ListNode(0)
        q = PriorityQueue()
        for l in lists:
            if l: 
                q.put((l.val,l))
        while not q.empty():
            p.next = q.get()[1]
            p = p.next
            if p.next:
                q.put((p.next.val,p.next))
        return head.next

31. Next Permutation(Medium)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Examples:
1,2,31,3,2
3,2,11,2,3
1,1,51,5,1

# my solution, T(n)=O(N), S(n)=O(1)
# Runtime: 44 ms, faster than 97.48% of Python3 
class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        def swap(nums,i,j):
            temp=nums[j]
            nums[j]=nums[i]
            nums[i]=temp
        def reverse(nums,start): # python中list的内置rerverse只能反转整体nums
            i,j=start,len(nums)-1
            while(i<j):
                swap(nums,i,j)
                i+=1
                j-=1
        i,j = len(nums)-2,len(nums)-1
        while i>=0 and nums[i+1]<=nums[i]: # O(n)
            i-=1
        if i>-1:
            while i<j and nums[j]<=nums[i]: # O(n)
                j-=1
            swap(nums,i,j)
        reverse(nums,i+1) # O(n)        

32. Longest (Continuous) Valid Parentheses(Hard)

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
Example :
Input: "(()" , Output: 2
Input: ")()())" , Output: 4

# 感觉这个题目加上“连续”好些吧。。。做半天都理解错了
# 太难了,不会,

# Approach 3: Using Stack , T(n)=S(n)=O(n)
# Runtime: 36 ms, faster than 100.00% of Python3 ...过分了
class Solution:
    def longestValidParentheses(self, s: str) -> int:
        stack=[-1]
        ans=0
        for i,item in enumerate(s):
            if item =='(':
                stack.append(i)
            else:
                stack.pop()
                if stack==[]:
                    stack.append(i)
                else:
                    ans=max(ans,i-stack[-1])
        return ans

# Approach 4: Without extra space, T(n)=O(n), S(n)=O(1)
class Solution:
    def longestValidParentheses(self, s: str) -> int:
        l=r=ans=0
        for i,item in enumerate(s):
            if item=='(':
                l+=1
            else:
                r+=1
            if l==r:
                ans=max(ans,2*r)
            elif r>=l:
                l=r=0
        l=r=0
        for i in range(len(s)-1,0,-1):
            if s[i]=='(':
                l+=1
            else:
                r+=1
            if l==r:
                ans=max(ans,2*l)
            elif l>=r:
                l=r=0
        return ans

33. Search in Rotated Sorted Array(Medium)

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 array return its index, otherwise return -1.
!!!You may assume no duplicate exists in the array.
!!!Your algorithm's runtime complexity must be in the order of O(log n).
Example:
Input: nums = [4,5,6,7,0,1,2], target = 0 , Output: 4
Input: nums = [4,5,6,7,0,1,2], target = 3 , Output: -1

# 暴力遍历,T(n)=O(n),S(n)=O(1)
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        n=len(nums)
        if n==0:
            return -1
        if target==nums[0]:
            return 0
        elif n==1:
            return -1
        if target==nums[-1]:
            return n-1
        i,j=1,n-2
        if target>nums[0]:
            while i<n and nums[i-1]<nums[i]:
                if target==nums[i]:
                    return i
                i+=1
        elif target<nums[-1]:
            while j>0 and nums[j]<nums[j+1]:
                if target==nums[j]:
                    return j
                j-=1
        return -1

# 二分查找,T(n)=O(logn),S(n)=O(1)
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        lo,hi=0,len(nums)-1
        while lo<hi:
            mid=(lo+hi)//2
            if nums[mid]>nums[hi]:
                lo=mid+1
            else:
                hi=mid
        rot=lo # lo==hi is the index of the smallest value
        lo,hi=0,len(nums)-1
        while lo<=hi:
            mid=(lo+hi)//2
            realmid=(mid+rot)%len(nums) # ?
            if nums[realmid]==target:
                return realmid
            if nums[realmid]<target: 
                lo=mid+1
            else:
                hi=mid-1
        return -1     

34. Find First and Last Position of Element in Sorted Array(Medium)

Given an array of integers nums 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 is not found in the array, return [-1, -1].
Example:
Input: nums = [5,7,7,8,8,10], target = 8 , Output: [3,4]
Input: nums = [5,7,7,8,8,10], target = 6 , Output: [-1,-1]

# 二分查找,T(n)=O(logn),S(n)=O(1)
# Runtime: 40 ms, faster than 87.82% of Python3 o
class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        left=right=-1
        # find the lowwer index of target
        low,high=0,len(nums)-1
        while low <= high:
            mid = low + (high - low) // 2;
            if target <= nums[mid]:
                high = mid - 1;
            else:
                low = mid + 1;
        if low < len(nums) and nums[low] == target:
            left=low
        # find the upper index of target
        low,high=0,len(nums)-1
        while low <= high:
            mid = low + (high - low) // 2;
            if target >= nums[mid]:
                low = mid + 1;
            else:
                high = mid - 1;
        if high > -1 and nums[high] == target:
            right=high
        return [left,right]

39. Combination Sum(Medium)

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

# 回溯法啊亲!!!!!!!!总是不会用!!!
# Runtime: 56 ms, faster than 99.41% of Python3 
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def dfs(nums, target, temp, ans):
            if target == 0:
                ans += [temp]
            else:
                for num in nums:
                    if num > target: 
                        break
                    if temp!=[] and num < temp[-1]: 
                        continue
                    dfs(nums, target - num, temp + [num], ans)
        ans = []
        dfs(sorted(candidates), target, [], ans)
        return ans

42. Trapping Rain Water(Hard)

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
Approach 2: Dynamic Programming
# Approach 1: Brute force , T(n)=O(n^2) , S(n)=O(1) , 超时
class Solution:
    def trap(self, height: List[int]) -> int:
        ans=0
        for i,current in enumerate(height):
            maxleft=maxright=0
            for left in height[:i+1]:
                maxleft=max(maxleft,left)
            for right in height[i:]:
                maxright=max(maxright,right)
            ans+=min(maxleft,maxright)-current
        return ans

# Approach 2: Dynamic Programming , T(n)=S(n)=O(n)
import copy
class Solution:
    def trap(self, height: List[int]) -> int:
        ans,n=0,len(height)
        if n==0:
            return 0
        leftmax,rightmax=copy.copy(height),copy.copy(height) # 浅复制,不改变源列表
        for i in range(1,n):
            leftmax[i]=max(height[i],leftmax[i-1])
        for i in range(n-2,-1,-1):
            rightmax[i]=max(height[i],rightmax[i+1])
        for i in range(1,n-1):
            ans+=min(leftmax[i],rightmax[i])-height[i]
        return ans

# Approach 4: Using 2 pointers , T(n)=O(n) , S(n)=O(1) 
class Solution:
    def trap(self, height: List[int]) -> int:
        ans=leftmax=rightmax=0
        i,j=0,len(height)-1
        while i<j:
            if height[i]<height[j]:
                if height[i]>=leftmax:
                    leftmax=height[i]
                else:
                    ans+=leftmax-height[i]
                i+=1
            else:
                if height[j]>=rightmax:
                    rightmax=height[j]
                else:
                    ans+=rightmax-height[j]
                j-=1
        return ans
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,267评论 0 10
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,700评论 0 38
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,785评论 0 0
  • 今日七月十六日(农历)。 圆圆的一轮月亮从晚九点升起,月光如水,月光如银,月光如少女般轻盈,她款款的跨进我的纱窗,...
    开拓者111阅读 524评论 3 20
  • 前几天郭敬明事件上了热搜,但是我并没有点击进去看,连看热闹的心情都没有,这就是我对于郭敬明的态度。今天写下这篇文章...
    八月初上阅读 164评论 0 0