From 4a4236fdb4b779f0be1a66347424991cd0a86229 Mon Sep 17 00:00:00 2001 From: Powerstot <77142630+Powerstot@users.noreply.github.com> Date: Wed, 12 May 2021 10:50:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0015.三数之和 --- problems/0015.三数之和.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 0bd683a8..1dabe885 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -178,7 +178,44 @@ public: Java: +```Java +class Solution { + public List> threeSum(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + for (int i = 0; i < nums.length; i++) { + if (nums[i] > 0) { + return result; + } + + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + int left = i + 1; + int right = nums.length - 1; + while (right > left) { + int sum = nums[i] + nums[left] + nums[right]; + if (sum > 0) { + right--; + } else if (sum < 0) { + left++; + } else { + result.add(Arrays.asList(nums[i], nums[left], nums[right])); + + while (right > left && nums[right] == nums[right - 1]) right--; + while (right > left && nums[left] == nums[left + 1]) left++; + + right--; + left++; + } + } + } + return result; + } +} +``` Python: From 4ceab173637ff45eb2213160c960968cddb8fb73 Mon Sep 17 00:00:00 2001 From: Powerstot <77142630+Powerstot@users.noreply.github.com> Date: Wed, 12 May 2021 13:23:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00015.=E4=B8=89=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0015.三数之和 Java版本 --- problems/0015.三数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 1dabe885..55e22887 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -207,7 +207,7 @@ class Solution { while (right > left && nums[right] == nums[right - 1]) right--; while (right > left && nums[left] == nums[left + 1]) left++; - right--; + right--; left++; } }