From 53a8669da9d352978395027b1cd3f6e49a32f76d Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Fri, 16 Jul 2021 22:18:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=2053=20-=20I:?= =?UTF-8?q?=E5=9C=A8=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E6=95=B0=E5=AD=97=20I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.java | 67 +++++++++++++++++++ .../cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.md | 24 +++++++ 2 files changed, 91 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.java create mode 100644 src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.md diff --git a/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.java b/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.java new file mode 100644 index 0000000..a16eef1 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.java @@ -0,0 +1,67 @@ +//统计一个数字在排序数组中出现的次数。 +// +// +// +// 示例 1: +// +// 输入: nums = [5,7,7,8,8,10], target = 8 +//输出: 2 +// +// 示例 2: +// +// 输入: nums = [5,7,7,8,8,10], target = 6 +//输出: 0 +// +// +// +// 限制: +// +// 0 <= 数组长度 <= 50000 +// +// +// +// 注意:本题与主站 34 题相同(仅返回值不同):https://leetcode-cn.com/problems/find-first-and-last- +//position-of-element-in-sorted-array/ +// Related Topics 数组 二分查找 +// 👍 179 👎 0 + +package leetcode.editor.cn; + +//剑指 Offer 53 - I:在排序数组中查找数字 I +class ZaiPaiXuShuZuZhongChaZhaoShuZiLcof { + public static void main(String[] args) { + //测试代码 + Solution solution = new ZaiPaiXuShuZuZhongChaZhaoShuZiLcof().new Solution(); + System.out.println(solution.search(new int[]{5, 7, 7, 8, 8, 10}, 6)); + System.out.println(solution.search(new int[]{5, 7, 7, 8, 8, 10}, 8)); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int search(int[] nums, int target) { + int start = binarySearch(nums, target, true); + int end = binarySearch(nums, target, false) - 1; + if (start <= end && end < nums.length && nums[start] == target && nums[end] == target) { + return end - start + 1; + } + return 0; + } + + public int binarySearch(int[] nums, int target, boolean lower) { + int start = 0, end = nums.length - 1, ans = nums.length; + while (start <= end) { + int mid = (start + end) / 2; + if (nums[mid] > target || (lower && nums[mid] >= target)) { + end = mid - 1; + ans = mid; + } else { + start = mid + 1; + } + } + return ans; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.md b/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.md new file mode 100644 index 0000000..13163a4 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ZaiPaiXuShuZuZhongChaZhaoShuZiLcof.md @@ -0,0 +1,24 @@ +

统计一个数字在排序数组中出现的次数。

+ +

 

+ +

示例 1:

+ +
输入: nums = [5,7,7,8,8,10], target = 8
+输出: 2
+ +

示例 2:

+ +
输入: nums = [5,7,7,8,8,10], target = 6
+输出: 0
+ +

 

+ +

限制:

+ +

0 <= 数组长度 <= 50000

+ +

 

+ +

注意:本题与主站 34 题相同(仅返回值不同):https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

+
Related Topics
  • 数组
  • 二分查找
  • \n
  • 👍 179
  • 👎 0
  • \ No newline at end of file