LeetCode矩阵类训练

LeetCode矩阵类训练

4. Median of Two Sorted Arrays
描述:
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
解:
主要是归并排序法【意会吧】。

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        res = []
        l = 0
        r = 0
        while l<len(nums1) and r<len(nums2):
            if nums1[l] < nums2[r]:
                res.append(nums1[l])
                l += 1
            else:
                res.append(nums2[r])
                r += 1
        res += list(nums1[l:])
        res += list(nums2[r:])
        k = len(res)
        if k%2 == 0:
            med = (res[k/2-1]+res[k/2])/2.0
        else:
            med = float(res[k/2])
        return med
  

15.3Sum:
描述
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
解:利用二叉树的方法来做。将正负数值分为两类,然后得到第三个数,判断第三个数是否在数组中。存在的坑是:
判断第三个数a是不是在nums中,并且a的个数是不是>1个以上。另外就是a没有重复的情况下的list的存储格式。还有注意[0,0,0]的形式。

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        b = []
        n = len(nums)
        if n<3:
            return b
        data = set(nums)
        print data
        neg = [x for x in data if x<0]
        pos = [x for x in data if x>=0]
       # print neg
     #   print pos
        if nums.count(0)>=3:
            b.append([0, 0, 0])
        for i in neg:
            for j in pos:
                a = -i-j
   #             print a
                if a in data:
                    if a in [i, j] and nums.count(a) > 1:
                        b.append([i, a, j])
                    elif a>i and a<j:
                        b.append([i, a, j])
                  #  elif a > j:
                   #     b.append([i, j, a])
                 #   elif a < i:
                  #      b.append([a, i, j])
        return b  

11. Container With Most Water
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
参考了其他的答案,其实还是不太明白。是动态规划问题。

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        """
        area = 0
        n = len(height)
        for i in range(n-1):
            for j in range(i, n):
                 area = max(area, (r-l)*min(height[l], height[r]))
        return area
        """
        n = len(height)
        l = 0
        r = n-1
        area = (r-l)*min(height[l], height[r])
        while (r-l)>0 :
            area = max(area, (r-l)*min(height[l], height[r]))
            if height[r] > height[l]:
                l +=1
            else:
                r -= 1
        return area   

970. Powerful Integers
Given two positive integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.
Return a list of all powerful integers that have value less than or equal to bound.
You may return the answer in any order. In your answer, each value should occur at most once.
Example 1:
Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
Example 2:
Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
Note:
1 <= x <= 100
1 <= y <= 100
0 <= bound <= 10^6

class Solution(object):
    def powerfulIntegers(self, x, y, bound):
        """
        :type x: int
        :type y: int
        :type bound: int
        :rtype: List[int]
        """
        res = []
        if x == 1:
            m = 1
        else:
            m = int(math.log(bound, x))+1
        if y == 1:
            n = 1
        else:
            n = int(math.log(bound, y))+1
        for i in range(m+1):
            for j in range(n+1):
                Sum = x**i + y**j
                if Sum <= bound:
                    res.append(Sum)
        return set(res)

用set()剔除重复的结果。
如果没有math.log()。可以使m=17, n=17,因为2^17=131072,
2^16=65536。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值