leetCode 15. 3Sum (3数之和) 解题思路和方法
来源:互联网 发布:网络推广要做什么 编辑:程序博客网 时间:2024/05/16 04:45
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, a ≤ b ≤ c)
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)
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, a ≤ b ≤ c)
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)
思路:此题解法上不算难,但是通过率并不高,只有16.9%,显然在其他地方存在限制,果然第一次提交测试的时候,果断TLE(超时)。代码效率上需要更快的速度。
第一次代码本地测试一组8ms左右,不能过,后面上网参看资料,写出改进的方法,相同的数据,本地测试2ms左右,效率约提高了4倍。
第一种方法代码:
public class Solution { public List<List<Integer>> threeSum(int[] nums) { if(nums.length < 2){ return null; } //System.out.println(nums); Arrays.sort(nums); List<List<Integer>> list = new ArrayList<List<Integer>>(); for(int i = 0; i < nums.length - 2; i++){ if(nums[i] > 0) break; if(i > 1 && nums[i] == nums[i-1]){ continue; } for(int j = nums.length - 1; j > i + 1; j--){ if(nums[j] < 0) break; if(j < nums.length -1 && nums[j] == nums[j+1]){ continue; } //System.out.println(nums[i]); int c = -(nums[i] + nums[j]); int k = search(c,nums,i+1,j-1); if(k > 0){ List<Integer> al = new ArrayList<Integer>(); al.add(nums[i]); al.add(nums[k]); al.add(nums[j]); list.add(al); } } } return list; } //二分查找数值c的位置,找到返回位置;找不到返回-1 public static int search(int c,int[] nums,int start,int end){ if(c < nums[start] || c > nums[end]) return -1; int k = 0; while(start <= end){ k = (start + end)/2; System.out.println(k); if(c > nums[k]){ start = k + 1; }else if(c < nums[k]){ end = k - 1; } else{ return k; } } return -1; }}第二种方法代码:
public class Solution {public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> list = new ArrayList<List<Integer>>(); if(nums.length < 2){ return list; } Arrays.sort(nums);//数组排序 int j,k,m; int len = nums.length; for(int i = 0; i < len - 2; i++){ //如果最小值依然大于0或者最大值小于0,肯定没有符合要求的值,直接返回 if(nums[i] > 0 || nums[len-1] < 0) break; //如果现在的数字和之前的数字重复,直接跳出,继续下一下 if(i > 0 && nums[i] == nums[i-1]){ continue; } //初始值 j = i + 1; k = len - 1; while(j < k){ m = nums[i] + nums[j] + nums[k];//三者之和 if(m == 0){//=0,满足条件 List<Integer> al = new ArrayList<Integer>(); al.add(nums[i]); al.add(nums[j]); al.add(nums[k]); list.add(al); j++; k--; //如果相邻数字相等,则直接跳过,此处重要 while(j < k && nums[j] == nums[j-1]){ j++; } while(j < k && nums[k] == nums[k+1]){ k--; } }else{ if(m > 0)//这里也是很重要的点,分情况位置标记变动 k--; else j++; } } } return list; }}
0 0
- leetCode 15. 3Sum (3数之和) 解题思路和方法
- leetCode 16.3Sum Closest (离给定值最近的三数之和) 解题思路和方法
- Leetcode #15 3Sum 三数之和 解题小节
- Leetcode: 15. 3Sum 三数之和
- LeetCode 3sum 解题思路
- leetcode-3sum 三数之和
- [LeetCode] 3Sum 三数之和 Python
- leetCode 18.4Sum (4数字和) 解题思路和方法
- Leetcode #16 3Sum Closest 找3数之和最接近 解题小节
- LeetCode 15. 3Sum(三数之和)
- leetCode 1. Two Sum 解题思路和方法
- leetCode 39.Combination Sum(组合总和) 解题思路和方法
- 3Sum 数组找3数之和为0 @LeetCode
- [LeetCode] 3Sum Closest 最近的三数之和 Python
- LeetCode 15. 3Sum 三个正数之和
- 3Sum 三个数之和
- leetCode 112.Path Sum (路径和) 解题思路和方法
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- 2012年5月SAT香港真题解析
- VC++2008中快速运用OpenCV打开图像文档
- 后海日记(11)
- iOS ——网络监测
- 【从零单排之微软面试100题系列】02之包含 min 函数的栈
- leetCode 15. 3Sum (3数之和) 解题思路和方法
- 使用Visio—UML画类图
- work——内容
- Web前端_jQuery_命名空间
- jquery的trigger和triggerHandler区别
- STM32F0项目进阶之实时时钟DS1307
- 8天学通MongoDB——第五天 主从复制
- [Leetcode]-Merge Sorted Array
- hdu 5277 YJC counts stars