15. 3Sum java solutions

Given an array S of n integers, are there elements abc 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, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

Subscribe to see which companies asked this question

 1 public class Solution {
 2     List<List<Integer>> ans = new ArrayList<List<Integer>>();
 3     public List<List<Integer>> threeSum(int[] nums) {
 4         if(nums == null || nums.length < 3) return ans;
 5         Arrays.sort(nums);
 6         for(int i = 0; i < nums.length - 2; i++){
 7             if(i > 0 && nums[i] == nums[i-1]) continue;
 8             find3sum(i+1,nums.length-1,nums,0-nums[i]);
 9         }
10         return ans;
11     }
12
13     public void find3sum(int s,int e,int[] nums,int target){
14         while(s < e){
15             int sum = nums[s] + nums[e];
16             if(sum == target){
17                 List<Integer> tmp = new ArrayList<Integer>();
18                 tmp.add(0-target);
19                 tmp.add(nums[s++]);
20                 tmp.add(nums[e--]);
21                 ans.add(tmp);
22                 while(s < nums.length-1 && nums[s] == nums[s-1])s++;
23                 while(e >= 0 && nums[e] == nums[e+1])e--;
24             }else if(sum < target) s++;
25             else e--;
26         }
27     }
28 }

使用求解2sum 的方式来做,要注意的是重复元素的处理。先排序,然后略过重复元素

时间: 2024-10-05 06:09:51

15. 3Sum java solutions的相关文章

[LeetCode] 15. 3Sum Java

题目: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 = [-

47. Permutations II java solutions

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

54. Spiral Matrix java solutions

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. Subscribe to see which c

第15篇-JAVA JDBC编程

第15篇-JAVA JDBC编程 每篇一句 :对于勇敢者,尝试是一条崭新的生活之路 初学心得: 但对于坎坷与泥泞,能以平常之心视之,就非常不容易 (笔者:JEEP/711)[JAVA笔记 | 时间:2017-05-08| JAVA JDBC编程 ] 1.JDBC概述 通过使用JDBC API,Java程序可以非常方便地操作各种主流数据库,这是是Java语言的巨大魅力所在 由于Java语言的跨平台特性,所以使用JDBC API所编写的程序不仅可以实现跨数据库,还可以跨平台,具有非常优秀的可移植性

15套java互联网架构师、高并发、集群、负载均衡、高可用、数据库设计、缓存、性能优化、大型分布式 项目实战视频教程

* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布 式项目实战视频教程 视频课程包含: 高级Java架构师包含:Spring boot.Spring  cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat

1. Two Sum&amp;&amp;15. 3Sum&amp;&amp;18. 4Sum

题目: 1. Two Sum 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. Given nums = [2, 7, 1

119. Pascal&#39;s Triangle II java solutions

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3