LeetCode刷题——数组

LeetCode-1:

 

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

算法设计:

此题可以两个for循环遍历查找,但是复杂度太高;

不妨尝试用字典,单层for循环即可搞定:

每次遍历时把相反的数存入字典,则在遍历到key值时则返回。

代码:

for i in range(len(nums)):
            if nums[i] in dic:
               return [dic[nums[i]], i]

            dic[target - nums[i]] = i

 

 

leetCode-11:

给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (iai) 。画 n 条垂直线,使得垂直线 i 的两个端点分别为 (iai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

 算法设计:

记录左边/右边的两个坐标以及高度,哪一边小就使得一边往另一边方向移动, 并在移动的过程中不断的更新面积。

此题

代码如下:

def maxArea(self, height):
        left, right = 0, len(height) - 1
        maxarea, area = 0, 0
        while left < right:
            l, r = height[left], height[right]
            if l < r:
                area = (right - left) * l
                while height[left] <= l:
                    left += 1
            else:
                area = (right - left) * r
                while height[right] <= r and right:
                    right -= 1
            if area > maxarea:
                maxarea = area
        return maxarea

 

leetCode-15:

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

算法设计:

先对数组排序,遍历数组数i和i-1相同则继续遍历, 若令 l 等于左边i+1, r等于最右边,求三个数的和,若大于0(r-1),小于0则l + 1直到遍历和为0为止,和为0也可以同时l + 1, r-1看是否还有其他数使得和为0。

代码如下:

def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        res = []
        # sort list
        nums.sort()
        for i in range(len(nums)):
            l, r = i + 1, len(nums) - 1
            #judge edge cases
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            while l < r:
                S = nums[i] + nums[l] + nums[r]
                if S > 0:
                    r -= 1
                elif S < 0:
                    l += 1
                else:
                    res.append([nums[i], nums[l], nums[r]])
                    while l < r and nums[l] == nums[l + 1]:
                        l += 1
                    while l < r and nums[r] == nums[r - 1]:
                        r -= 1
                    l += 1 ; r -= 1
        return res

Leetcode16.最接近的三数之和

算法设计:

对数据列表排序,遍历列表数据,取l为该数据右边一位,r为列表最右一位,分别往中间遍历求和最接近目标的三个数。

源代码:

def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        result = nums[0] + nums[1] + nums[2]
        for i in range(len(nums) - 2):
            l, r = i + 1, len(nums) - 1
            while l < r:
                tmp = nums[i] + nums[l] + nums[r]
                if tmp == target:
                    return tmp
                if abs(tmp - target) < abs(result - target):
                    result = tmp
                
                if tmp < target:
                    l += 1
                elif tmp > target:
                    r -= 1
        
        return result

 

Leetcode18 

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

算法设计:

首先对num list排序,取i再取j 从i + 1开始遍历,l和r从j + 1和最后一个开始往回遍历。

源代码:

def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        # init param
        nums.sort()
        result = []
        # edge case
        if len(nums) < 4:
            return []
        for i in range(len(nums) - 3):
            if nums[i] * 4 > target:
                break
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            for j in range(i + 1, len(nums) - 2):
                if nums[j] * 3 > target - nums[i]:
                    break
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue
                key = target - nums[i] - nums[j]
                l, r = j + 1, len(nums) - 1
                while l < r:
                    if nums[i] == 0:
                        print "0"
                    if key < nums[l] + nums[r]:
                        r -= 1
                    elif key > nums[l] + nums[r]:
                        l += 1
                    else:
                        result.append([nums[i], nums[j], nums[l], nums[r]])
                        while l < r and nums[l] == nums[l + 1]:
                            l += 1
                        while l < r and nums[r] == nums[r - 1]:
                            r -= 1
                        l += 1
                        r -= 1
        return result

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值