zoukankan      html  css  js  c++  java
  • [leetcode]15. 3Sum三数之和

    Given an array nums of n integers, are there elements abc in nums 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.

    Example:

    Given array nums = [-1, 0, 1, 2, -1, -4],
    
    A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]

    题意:

    给定一个数组和一个值target,找到所有三数加起来等于target的组合

    Solution1:Two Pointers(left and right to meet each other)

    1. Sort the array (coz we must check the duplicates) 

    2. Lock one pointer and do two sum with the other two

    Step1: Sort the given array in ascending order

    Step2: Lock pointer i, then do two sum with two pointers j, k

    Step3:  checking nums[i] + nums[j] + nums[k] == target ? 

               if   nums[i] + nums[j] + nums[k] < target,  pointer j ++

               if   nums[i] + nums[j] + nums[k] > target,  pointer k --

                           

    注意几点:

    1、题目要求“The solution set must not contain duplicate triplets.” 每次移动 i , j , k 都注意查重

    2、Arrays工具类中常用方法需要牢记:

    Arrays.sort() 排序数组

    Arrays.fill() 填充数组

    Arrays.toString() 将int数组转成string数组

    Arrays.asList() 将数组转成list集合

    code:

     1 /*
     2     Time Complexity: O(n^2). For each locked item, we need traverse the rest of array behind such item.  
     3     Space Complexity: O(1).  We only used constant extra space.
     4 */
     5 class Solution {
     6     public List<List<Integer>> threeSum(int[] nums) {
     7         List<List<Integer>> result = new ArrayList<>();
     8         int target = 0;
     9         Arrays.sort(nums);
    10         // corner case
    11         if (nums.length < 3) return result;
    12 
    13         for (int i = 0; i < nums.length; i++) {
    14             if (i > 0 && nums[i] == nums[i - 1]) continue;   // skip duplicates
    15             int j = i + 1;
    16             int k = nums.length - 1;
    17             while (j < k) {
    18                 if (nums[i] + nums[j] + nums[k] < target) {
    19                     j++;
    20                     while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
    21                 } else if (nums[i] + nums[j] + nums[k] > target) {
    22                     k--;
    23                     while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
    24                 } else {
    25                     result.add(Arrays.asList(nums[i], nums[j], nums[k]));
    26                     j++;
    27                     k--;
    28                     while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
    29                     while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
    30                 }
    31             }
    32         }
    33         return result;
    34     }
    35 }
  • 相关阅读:
    查看docker程序使用的内存脚本
    shell分割字符串并赋值给变量
    【Ceph】Ceph学习理解Ceph的三种存储接口:块设备、文件系统、对象存储
    删除软连接导致源文件一起被删除
    nginx+keepalived实现双活
    maven私有仓库的搭建
    直接访问nginx ip地址返回404错误
    Solaris基础系列之四:图解Oracle 10g安装
    数据库进阶系列之一:漫谈数据库索引
    Tips&Tricks系列四:C#面试笔试小贴士
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9124596.html
Copyright © 2011-2022 走看看