class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length < 3){
return res;
}
Set<Integer> firstNum = new HashSet<>();
//[-1, 0, 1, 2, -1, -4]
//firstNum:-1
//i = 1 sum = 0
//secondNum:
//thirdNum:
//j = 2
//res:[-1,1,0] [-1, -1, 2]
for (int i = 0; i < nums.length; i++){
if (firstNum.contains(nums[i])){
continue;
}
int sum = -nums[i];
Set<Integer> secondNum = new HashSet<>();
Set<Integer> thirdNum = new HashSet<>();
for (int j = i + 1; j < nums.length; j++){
if (!firstNum.contains(nums[j]) && !thirdNum.contains(nums[j])){
if (secondNum.contains(sum - nums[j])){
res.add(new ArrayList<Integer>(Arrays.asList(nums[i], nums[j], sum - nums[j])));
thirdNum.add(nums[j]);
} else {
secondNum.add(nums[j]);
}
}
}
firstNum.add(nums[i]);
}
return res;
}
}
3sum 不sort
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內(nèi)容
- 關于解題:https://discuss.leetcode.com/topic/87252/solve-this-...
- 寫好分治排序后,腦里靈光一閃,想看看python的sort和numpy的sort還有自己的分治排序的效率,被完爆了...
- ? 注冊公司的時候會需要填寫注冊地址,而且是必須填寫項。注冊地址也就是辦公地址,大多數(shù)公司辦公地址是寫字樓,但由于...