题目链接:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/
执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:41.4 MB, 在所有 Java 提交中击败了36.74% 的用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class Solution { public int search(int[] nums, int target) { if (nums.length == 0) { return 0; } int left = 0, right = nums.length - 1; int start = 0, count = 0; while (left <= right) { int mid = left + (right-left)/2; if (nums[mid] == target) { start = mid; break; } else if (nums[mid] > target) { right--; } else { left++; } } right = start; left = start-1; while (right < nums.length) { if (nums[right] == target) { count++; } else { break; } right++; } while (left >= 0) { if (nums[left] == target) { count++; } else { break; } left--; } return count; } }
|