56. Merge Intervals
Medium

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

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

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

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就可以了

posted on 2019-09-16 08:50  黑暗尽头的超音速炬火  阅读(121)  评论(0编辑  收藏  举报