Python-Leetcode(第八,九周作业)

11. Container With Most Water

Description

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:
    def maxArea(self, height):
        left = 0
        right = len(height) - 1
        maxarea = 0
        while 0 <= left < right <= len(height) - 1:
            maxarea = max(maxarea, min(height[left], height[right]) * (right - left))
            if height[left] > height[right]:
                right -= 1
            else:
                left += 1
        return maxarea     

16. 3Sum Closest

Description

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

解题思路

先对数组进行排序,然后采用迭代的方法求解.
对于长度为n的数组,排序后,固定第一个数的范围,规定第二第三个数在第一个数后面,那么第一个数的下标范围为[0,n-3].
对第一个数进行迭代,取第一个数下标为i,则第二个数下标为i+1,第三个数恒定取下标为length-1,对三个数求和.当求和与target相比要小时,增加第二个数下标;当求和与target相比要大时,减少第三个数下标,不断减少与 target的差距,每次去对比差距并适时更新与target相差的最小值.
通过这样的迭代,最后就可以找到答案,并且复杂度只有 O(n2) O ( n 2 ) .

代码

class Solution:
    # @return an integer
    def threeSumClosest(self, num, target):
        num.sort()
        result = num[0] + num[1] + num[2]
        for i in range(len(num) - 2):
            j, k = i+1, len(num) - 1
            while j < k:
                sum = num[i] + num[j] + num[k]
                if sum == target:
                    return sum
                if abs(sum - target) < abs(result - target):
                    result = sum
                if sum < target:
                    j += 1
                elif sum > target:
                    k -= 1
        return result

66. Plus One

Description

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

解题思路

这个直接迭代判断就好了,如果遇到最高位进位,就在列表前面插入一个1就好了

代码

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        pos=len(digits)-1
        digits[pos]+=1
        while pos>=1 :
            if digits[pos] == 10:
                digits[pos]=0
                digits[pos-1]+=1
            pos-=1
        if digits[0]==10:
            digits[0]=0
            digits.insert(0,1)
        return digits

338.Counting Bits

解题思路

如果已知0到 2k1 2 k − 1 之间的所有数的比特计数,那么 2k 2 k 2k+11 2 k + 1 − 1 之间的所有数的比特计数就可以推导出来: bit[i+2k]=bit[i]+1 b i t [ i + 2 k ] = b i t [ i ] + 1 ,其中 0<=i<2k 0 <= i < 2 k .
Tips: bit[i+2k] b i t [ i + 2 k ] 的前k位与 bit[i] b i t [ i ] 相同,只是第k+1位由0变成了1.

代码

class Solution:
    def countBits(self, num):
        """
        :type num: int
        :rtype: List[int]
        """
        if num == 0:
            return [0]
        ans = [0, 1]
        n = 2
        while n <= num:
            for i in range(0, n):
                ans.append(ans[i] + 1)
            n <<= 1
        return ans[:num + 1]

你可能感兴趣的:(Python)