015 3 SUM

这道题县排序 然后设定好第一位,后面两位夹逼搜索就好, 但是因为数字会出现重复,导致得出的3元祖也有可能重复,一开始我的办法是采用set来避免多次加入同样的三元组,运行时间为 344ms

class Solution:
    # @param {integer[]} nums
    # @return {integer[][]}
    def threeSum(self, nums):
        length = len(nums)
        if length <= 2:
            return []
        nums = sorted(nums)
        ans = set()
        for i in range(0, length - 2):
            j = i + 1
            k = length - 1
            while j < k:
                s = nums[i] + nums[j] + nums[k]
                if s == 0:
                    ans.add((nums[i], nums[j], nums[k]))
                    j += 1
                elif s < 0:
                    j += 1
                else:
                    k -= 1
        ret = []
        for a in ans:
            ret.append(list(a))
        return ret

看了下Python runtime distributtion 发现有一些python解法明显比我快, 因此优化了一下避免重复加入3元组的部分, 不使用set, 而是在夹逼时如果发现相邻两个数相等则不继续求和判断,结果运行时间 212ms 有了明显提高,代码如下

class Solution:
    # @param {integer[]} nums
    # @return {integer[][]}
    def threeSum(self, nums):
        length = len(nums)
        if length <= 2:
            return []
        nums = sorted(nums)
        ans = []
        i = 0
        while i < length - 2:
            j = i + 1
            k = length - 1
            while j < k:
                s = nums[i] + nums[j] + nums[k]
                if s == 0:
                    ans.append([nums[i], nums[j], nums[k]])
                    j += 1
                    k -= 1
                    while j < k and nums[j] == nums[j-1]:
                        j += 1
                    while j < k and nums[k] == nums[k+1]:
                        k -= 1
                elif s < 0:
                    j += 1
                else:
                    k -= 1
            i += 1
            while i < length - 2 and nums[i] == nums[i-1]:
                i += 1
        return ans
时间: 2024-08-07 21:08:06

015 3 SUM的相关文章

JAVA经典算法40题(1-20)

[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?   1.程序分析:   兔子的规律为数列1,1,2,3,5,8,13,21....   public class exp2{ public static void main(String args[]){  int i=0;  for(i=1;i<=20;i++)   System.out.println(f(i)); } publ

Java经典算法案例

笔试中的编程题3 JAVA经典算法40例[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... public class exp2{public static void main(String args[]){int i=0;for(i=1;i<=20;i++)System.out.println(f(i));}pu

016 3SUM Closest

这道题与 015 3 SUM 基本思路一样 都是夹逼思想  复杂度 O(n*n) class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def threeSumClosest(self, nums, target): length = len(nums) nums = sorted(nums) ans = sum(nums[0:3]) i = 0 while i < leng

JAVA经典算法50题(转)

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51097928 JAVA经典算法50题 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21.... [java] view plain copy public class Demo01 { public s

JAVA的经典算法题

资料来源:http://blog.csdn.net/l1028386804/article/details/51097928 [程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21.... public class Demo01 { public static void main(String args[]) { for

JAVA经典算法40题

[程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不 死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i<=20;i++) System.out.println(f(i)); }public static int

【转】JAVA经典算法40题

转自:http://blog.csdn.net/chrp99/article/details/8771592 [程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....public class exp2{ public static void main(String args[]){ int i=0; for(i=1;i<

40道Java初中级算法面试题

[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析:   兔子的规律为数列1,1,2,3,5,8,13,21.... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class exp2{     public stat

35、java经典算法50道

[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21.... public class Demo01 { public static void main(String args[]) { for (int i = 1; i <= 20; i++) System.out.println(f(i)); } public sta