当前位置: 首页 > news >正文

[leetcode] 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:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, abc)
  • The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

https://oj.leetcode.com/problems/3sum/

思路1:先排序(用于结果展示和去重),对于每个元素target(注意去重),找另外两个元素使得和为-target,就转化成了n次求2Sum。复杂度O(n^2)。

NSum扩展:可参考 这里

 

import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (num == null || num.length < 3)
            return result;
        int n = num.length;
        Arrays.sort(num);
        for (int i = 0; i < n; i++) {
            int target = -num[i];
            int p = i + 1, q = n - 1;
            while (p < q) {
                if (num[p] + num[q] < target)
                    p++;
                else if (num[p] + num[q] > target)
                    q--;
                else {
                    ArrayList<Integer> tmp = new ArrayList<Integer>();
                    tmp.add(-target);
                    tmp.add(num[p]);
                    tmp.add(num[q]);
                    result.add(tmp);
                    p++;
                    q--;
                    // remove duplicates
                    while (p < n && num[p] == num[p - 1])
                        p++;
                    while (q >= i + 1 && num[q] == num[q + 1])
                        q--;

                }

            }
            // remove duplicates
            while (i < n - 1 && num[i + 1] == num[i])
                i++;

        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().threeSum(new int[] { 0 }));
        System.out.println(new Solution().threeSum(new int[] { -1, 0 }));
        System.out.println(new Solution().threeSum(new int[] { 0, 1, -1, }));
        System.out.println(new Solution().threeSum(new int[] { 0, 1, 1, }));

        System.out.println(new Solution().threeSum(new int[] { -1, 0, 1, 2, -1, -4 }));
        System.out.println(new Solution().threeSum(new int[] { 0, 0, 0, 0, 1, -1 }));
        System.out.println(new Solution().threeSum(new int[] { -7, -1, -13, 2, 13, 2, 12, 3, -11, 3, 7, -15, 2, -9,
                -13, -13, 11, -10, 5, -13, 2, -12, 0, -8, 8, -1, 4, 10, -13, -5, -6, -4, 9, -12, 5, 8, 5, 3, -4, 9, 13,
                10, 10, -8, -14, 4, -6, 5, 10, -15, -1, -3, 10, -15, -4, 3, -1, -15, -10, -6, -13, -9, 5, 11, -6, -13,
                -4, 14, -3, 8, 1, -4, -5, -12, 3, -11, 7, 13, 9, 2, 13, -7, 6, 0, -15, -13, -11, -8, 9, -14, 1, 11, -7,
                13, 0, -6, -15, 11, -6, -2, 4, 2, 9, -15, 5, -11, -11, -11, -13, 5, 7, 7, 5, -10, -7, 6, -7, -11, 13,
                9, -10, -9 }));
        System.out.println(new Solution().threeSum(new int[] { -2, 0, 1, 1, 2 }));
        System.out.println(new Solution().threeSum(new int[] { -4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6 }));
    }
}

 

第二遍记录:

  双指针法的运用。

  如何巧妙的去重。

 

参考:

http://tech-wonderland.net/blog/summary-of-ksum-problems.html

http://www.cnblogs.com/TenosDoIt/p/3649607.html

 

 

 

 

转载于:https://www.cnblogs.com/jdflyfly/p/3810688.html

相关文章:

  • 实战CentOS-6.6安装流程
  • idea 使用
  • oracle 数据库相关名词--图解
  • Python服务器开发二:Python网络基础
  • Android Studio 使用本地gradle配置详解
  • CSS规范命名
  • Redis命令详解:Lists
  • 深度学习中的注意力机制
  • PV与并发之间换算的算法 换算公式
  • 序列化组件
  • 日常遇到的问题
  • 添加sudo免密码
  • 利用交通在手数据为换乘添加关注
  • qt注册表关联文件格式
  • [AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)
  • Facebook AccountKit 接入的坑点
  • js对象的深浅拷贝
  • php ci框架整合银盛支付
  • Python进阶细节
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • 码农张的Bug人生 - 见面之礼
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 我与Jetbrains的这些年
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 责任链模式的两种实现
  • #### golang中【堆】的使用及底层 ####
  • #systemverilog# 之 event region 和 timeslot 仿真调度(十)高层次视角看仿真调度事件的发生
  • #微信小程序(布局、渲染层基础知识)
  • #预处理和函数的对比以及条件编译
  • $GOPATH/go.mod exists but should not goland
  • (1)(1.13) SiK无线电高级配置(五)
  • (4)事件处理——(7)简单事件(Simple events)
  • (done) 两个矩阵 “相似” 是什么意思?
  • (Forward) Music Player: From UI Proposal to Code
  • (javaweb)Http协议
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (三)c52学习之旅-点亮LED灯
  • .Net - 类的介绍
  • .Net 6.0 处理跨域的方式
  • .NET C# 使用 iText 生成PDF
  • .net core 外观者设计模式 实现,多种支付选择
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • @Autowired 和 @Resource 区别的补充说明与示例
  • [100天算法】-不同路径 III(day 73)
  • [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存...
  • [2024] 十大免费电脑数据恢复软件——轻松恢复电脑上已删除文件
  • [Android] Upload package to device fails #2720
  • [Android实例] 保持屏幕长亮的两种方法 [转]
  • [BUG] Hadoop-3.3.4集群yarn管理页面子队列不显示任务
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [c++] 什么是平凡类型,标准布局类型,POD类型,聚合体