LeetCode开心刷题四十二天——56. Merge Intervals78. Subsets(DFS或者二进制)代码没绕懂80. Remove Duplicates from Sorted Arr...

56. Merge Intervals
Medium
2509194FavoriteShare

Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].

Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
思路:
对所有按键值排序,看下一个是和它合并还是单独存储
class Solution(object):
    def merge(self, intervals):
        ans = []
        # 使用下标表示interval里的是按哪个排序
        for interval in sorted(intervals, key=lambda x: x[0]):
            # 如果初始或者当前与上一个足够拉开差距,直接压入
            if not ans or interval[0] > ans[-1][1]:
                ans.append(interval)
            # 否则扩展上一个ans的范围
            else:
                ans[-1][1] = max(ans[-1][1], interval[1])
        return ans

solu=Solution()
intervals=[[1,3],[2,6],[8,10],[15,18]]
print(solu.merge(intervals))

 

 

78. Subsets
Medium
232756FavoriteShare

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
解法没弄懂:
DFS模板
https://blog.csdn.net/fightforyourdream/article/details/12866861
https://blog.csdn.net/Chen_yuazzy/article/details/76423134
https://zhuanlan.zhihu.com/p/24986203
解法:
https://www.cnblogs.com/zuoyuan/p/3769892.html

 

80. Remove Duplicates from Sorted Array II
Medium
755579FavoriteShare

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,1,2,3,3],

Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.

It doesn't matter what values are set beyond the returned length.
class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:return 0
        start=nums[0]
        cnt=1
        ans=0
        for i in nums[1:]:
            if i==start:
                cnt+=1
                if cnt>2:
                    nums.remove(i)
            else:
                cnt=1
                start=i
        # ans+=cnt if cnt<=2 else 2
        return len(nums)


solu=Solution()
nums=[0,0,1,1,1,1,2,3,3]
print(solu.removeDuplicates(nums))

 

 

81. Search in Rotated Sorted Array II
Medium
791351FavoriteShare

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true

Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
二分的根源在于必须分到一边,就好比拉帮结派,必须选择一边,所以只要能判断好相应的条件。另一边else就可以了

转载于:https://www.cnblogs.com/Marigolci/p/11525461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值