zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):数组类:第15题:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。 注意:答案中不可以包含重复的三元组。

    题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。  注意:答案中不可以包含重复的三元组。

    思路:可以参考两个数求和的思路,但是需要考虑结果重复的情况。

    代码1:使用两个数求和思路,但是最后需要在结果中进行重复筛选,耗时很长。

    class Solution:
        def threeSum(self, nums: List[int]) -> List[List[int]]:
            nums.sort()
            length = len(nums)
            result = []
            result_temp = []
            result_new = []
            a = 0
            for i in range(length):
                a = nums[i]
                target = 0 - a
                if i >= 1 and nums[i] == nums[i - 1]:
                    break
                for j in range(length):
                    if i == j:
                        continue
                    else:
                        target1 = target - nums[j]
                        target2 = nums[j + 1 :]
                        if target1 in target2:
                            b = nums[j]
                            c = target1
                            result.append([a,b,c])
            length_list = len(result)
            if length_list > 0:
                for t in range(length_list):
                    result_temp.append(result[t].sort())
                index1 = 0
                index2 = 1
                result_new.append(result_temp[index1])
                while index1 + index2 < length_list:
                    if(result_temp[index1] == result_temp[index1 + index2]):
                        continue
                    else:
                        index1 = index1 + index2
                        index2 = 1
                        result_new.append(result_temp[index1])
                    index2 += 1
            return result_new
    代码2:使用三个指针来做,可以很好的规避结果重复的情况。
    class Solution:
        def threeSum(self, nums: List[int]) -> List[List[int]]:
            nums.sort()
            length = len(nums)
            result = []
            limit = length - 2
            for i in range(limit):
                if i >= 1 and nums[i] == nums[i - 1]:
                    continue
                j = i + 1
                t =  length - 1
                while j < t:
                    data = nums[i] + nums[j] + nums[t]
                    if data < 0:
                        j += 1
                    elif data > 0:
                        t -= 1
                    else:
                        result.append([nums[i],nums[j],nums[t]])
                        j += 1
                        t -= 1
                        while j < t and nums[j] == nums[j - 1]:
                            j += 1
                        while j < t and nums[t] == nums[t + 1]:
                            t -= 1
            return result
  • 相关阅读:
    javamail.providers not found
    vue.js实现购物车功能2.0版本
    vue.js实现购物车功能
    iframe高度自适应
    C语言 自定义字符串拼接函数
    php安装
    Apache安装
    python:爬虫初体验
    c:forEach 显示下拉框并回显
    数据结构 --- 线性表学习(php模拟)
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12721198.html
Copyright © 2011-2022 走看看