Leetcode 15. 3Sum(python)

先排序,再循环遍历,用双指针。

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        solution=[]
        nums.sort()
        for i in range(len(nums)-1):
            left=i+1
            right=len(nums)-1
            while left<right:
                val=nums[i]+nums[left]+nums[right]
                if val==0 and [nums[i],nums[left],nums[right]] not in solution:
                    solution.append([nums[i],nums[left],nums[right]])
                    left+=1
                    right-=1
                elif val<0:
                    left+=1
                else:
                    right-=1
        return solution

  

时间: 2024-08-01 10:44:28

Leetcode 15. 3Sum(python)的相关文章

【leetcode】Candy(python)

题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如下三种情形. 找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是在2的基础上加1,为3.令一个凹点4, 向左,5比4高,5的糖果应该是在4的基础上加 1,为2,这时我们发现冲突了,从凹点1

LeetCode OJ_题解(python):001-Two Sum 【Array】【Easy】

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. 输入一个数组和target,要在一个数组中找到两个数字,其和为t

LeetCode 15. 3Sum(三数之和)

Given an array S of n integers, are there elements a, b, c in S 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. For example, given array S = [-1,

LeetCode 15 3Sum(3个数的和)

翻译 给定一个有n个整数的数组S,是否存在三个元素a,b,c使得a+b+c=0? 找出该数组中所有不重复的3个数,它们的和为0. 备注: 这三个元素必须是从小到大进行排序. 结果中不能有重复的3个数. 例如,给定数组S={-1 0 1 2 -1 4},一个结果集为: (-1, 0, 1) (-1, -1, 2) 原文 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F

leetcode 156周赛(python)

两周了,第一次独立写完四个题目,开心(当然... 不是在考试时间内写完的,主要还是四个题知识图谱内,而且还不难吧),加油! 独一无二的出现次数 给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数. 如果每个数的出现次数都是独一无二的,就返回 true:否则返回 false. 示例 1: 输入:arr = [1,2,2,1,1,3] 输出:true 解释:在该数组中,1 出现了 3 次,2 出现了 2 次,3 只出现了 1 次.没有两个数的出现次数相同. 提示: 1 <= arr.len

LeetCode OJ_题解(python):027-Remove Element 【Array】【Easy】

题目: Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't mat

LeetCode OJ_题解(python):035-Search Insert Position【Array】【Easy】

题目: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2[1,3,5,6]

15. 3Sum(Array)

对数组的每一个数进行遍历,从这个数之后的数字中找出两个的sum等于这个数的相反数.找出这两个数的方法是从剩下list的两端开始查找,要是两端也就是lo+hi < sum的话lo就往前进一位.这中间也要注意的时,因为不能有重复,因此在找到一组相等的之后要判断lo跟lo+1是否一样,一样的话就要lo++,对hi也是同理. 1 class Solution { 2 public List<List<Integer>> threeSum(int[] nums) { 3 List<

[LeetCode]题解(python):015-3Sum

题目来源: https://leetcode.com/problems/3sum/ 题意分析: 这道题目是输入一个数组nums.找出所有的3个数使得这3个数之和为0.要求1.输出的3个数按小到大排序,2.3个数的组合不重复.比如输入[-1,0,1,2,-1,-4],返回的应该是[[-1,0,1],[-1,-1,2]]. 题目思路: 如果直接暴力解决,那么时间复杂度是(O(n^3)).这样会TLE. 看到这道题目,我回想了leetcode的第一题.第一题是给出一个数组和一个target,找出数组的