From 3839ed4c9383defcfbd229fe1ee5aff40b60d0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Mon, 28 Mar 2022 16:51:44 +0800 Subject: [PATCH] =?UTF-8?q?259:=E8=BE=83=E5=B0=8F=E7=9A=84=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ThreeSumSmaller.java | 80 +++++++++++++++++++ .../editor/cn/doc/content/ThreeSumSmaller.md | 37 +++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ThreeSumSmaller.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/ThreeSumSmaller.md diff --git a/src/main/java/leetcode/editor/cn/ThreeSumSmaller.java b/src/main/java/leetcode/editor/cn/ThreeSumSmaller.java new file mode 100644 index 0000000..eb87ec2 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ThreeSumSmaller.java @@ -0,0 +1,80 @@ +//给定一个长度为 n 的整数数组和一个目标值 target ,寻找能够使条件 nums[i] + nums[j] + nums[k] < target 成立的 +//三元组 i, j, k 个数(0 <= i < j < k < n)。 +// +// +// +// 示例 1: +// +// +//输入: nums = [-2,0,1,3], target = 2 +//输出: 2 +//解释: 因为一共有两个三元组满足累加和小于 2: +//  [-2,0,1] +// [-2,0,3] +// +// +// 示例 2: +// +// +//输入: nums = [], target = 0 +//输出: 0 +// +// 示例 3: +// +// +//输入: nums = [0], target = 0 +//输出: 0 +// +// +// +// 提示: +// +// +// n == nums.length +// 0 <= n <= 3500 +// -100 <= nums[i] <= 100 +// -100 <= target <= 100 +// +// Related Topics 数组 双指针 二分查找 排序 👍 106 👎 0 + +package leetcode.editor.cn; + +import java.util.Arrays; + +//259:较小的三数之和 +public class ThreeSumSmaller { + public static void main(String[] args) { + Solution solution = new ThreeSumSmaller().new Solution(); + // TO TEST + solution.threeSumSmaller(new int[]{-1, 1, -1, -1}, -1); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int threeSumSmaller(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return 0; + } + Arrays.sort(nums); + int sum; + int count = 0; + for (int i = 0; i < nums.length; i++) { + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + sum = nums[i] + nums[left] + nums[right]; + if (sum < target) { + count += (right - left); + left++; + } else { + right--; + } + } + } + return count; + } + } + +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/ThreeSumSmaller.md b/src/main/java/leetcode/editor/cn/doc/content/ThreeSumSmaller.md new file mode 100644 index 0000000..0b80e73 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/ThreeSumSmaller.md @@ -0,0 +1,37 @@ +

给定一个长度为 n 的整数数组和一个目标值 target ,寻找能够使条件 nums[i] + nums[j] + nums[k] < target 成立的三元组  i, j, k 个数(0 <= i < j < k < n)。

+ +

 

+ +

示例 1:

+ +
+输入: nums = [-2,0,1,3], target = 2
+输出: 2 
+解释: 因为一共有两个三元组满足累加和小于 2:
+     [-2,0,1]
+     [-2,0,3]
+
+ +

示例 2:

+ +
+输入: nums = [], target = 0
+输出: 0 
+ +

示例 3:

+ +
+输入: nums = [0], target = 0
+输出: 0 
+ +

 

+ +

提示:

+ + +
Related Topics
  • 数组
  • 双指针
  • 二分查找
  • 排序

  • 👍 106
  • 👎 0
  • \ No newline at end of file