摘要:双指针法的解法。然后用和夹逼找到使三数和为零的三数数列,放入结果数组。对于这三个数,如果循环的下一个数值和当前数值相等,就跳过以避免中有相同的解。
Problem
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.
NoticeElements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
ExampleFor example, given array S = {-1 0 1 2 -1 -4}, A solution set is:
(-1, 0, 1) (-1, -1, 2)Note
双指针法:O(n^2)的解法。
遍历第一个到倒数第三个数nums[i],作为三数数列的队首;
nums[i]后面的一个数作为三数数列中的第二个数nums[left];
数组中最后一个数nums[nums.length-1]作为三数数列中的第三个数nums[right]。
然后用left和right夹逼找到使三数和为零的三数数列,放入结果数组res。
对于这三个数,如果循环的下一个数值和当前数值相等,就跳过以避免res中有相同的解。
public class Solution { public ArrayListupdate 2018-9> threeSum(int[] A) { ArrayList > res = new ArrayList(); if (A.length < 3) return null; Arrays.sort(A); for (int i = 0; i <= A.length-3; i++) { int left = i+1, right = A.length-1; if (i != 0 && A[i] == A[i-1]) continue; while (left < right) { int sum = A[i]+A[left]+A[right]; if (sum == 0) { ArrayList temp = new ArrayList(); temp.add(A[i]); temp.add(A[left]); temp.add(A[right]); res.add(temp); left++; right--; while (left < right && A[left] == A[left-1]) left++; while (left < right && A[right] == A[right+1]) right--; } else if (sum < 0) left++; else right--; } } return res; } }
class Solution { public List> threeSum(int[] nums) { List
> res = new ArrayList<>(); if (nums == null || nums.length < 3) return res; Arrays.sort(nums); for (int i = 0; i < nums.length-2; i++) { if (i == 0 || (i > 0 && nums[i] != nums[i-1])) { int sum = 0-nums[i]; int l = i+1, r = nums.length-1; while (l < r) { int curSum = nums[l]+nums[r]; if (curSum == sum) { res.add(Arrays.asList(nums[i], nums[l++], nums[r--])); while (l < r && nums[l] == nums[l-1]) l++; while (l < r && nums[r] == nums[r+1]) r--; } else if (curSum < sum) { l++; } else { r--; } } } } return res; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65953.html
摘要:这个题和的做法基本一样,只要在循环内计算和最接近的和,并赋值更新返回值即可。 Problem Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three intege...
Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...
Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...
摘要:建立两个堆,一个堆就是本身,也就是一个最小堆另一个要写一个,使之成为一个最大堆。我们把遍历过的数组元素对半分到两个堆里,更大的数放在最小堆,较小的数放在最大堆。同时,确保最大堆的比最小堆大,才能从最大堆的顶端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...
摘要:首先,我们应该了解字典树的性质和结构,就会很容易实现要求的三个相似的功能插入,查找,前缀查找。既然叫做字典树,它一定具有顺序存放个字母的性质。所以,在字典树的里面,添加,和三个参数。 Problem Implement a trie with insert, search, and startsWith methods. Notice You may assume that all i...
阅读 2790·2021-10-08 10:12
阅读 3872·2021-09-22 15:45
阅读 2421·2019-08-30 15:52
阅读 2530·2019-08-29 18:44
阅读 2551·2019-08-29 12:37
阅读 1042·2019-08-26 13:36
阅读 2478·2019-08-26 13:34
阅读 1331·2019-08-26 12:20