题意:

image.png

解题思路:

  1. 思路:
  2. 1. 通过二分查找找到target后,继续左右移动,直到遇到边界停止
  3. 2. 最后返回目标值的最小跟最大边界值

PHP代码实现:

  1. class Solution {
  2. function findFirstAndLastPosition($nums, $target) {
  3. if ($nums == null) return [-1, -1];
  4. $start = -1;
  5. $end = -1;
  6. $n = count($nums);
  7. for ($i = 0; $i < $n; $i++) {
  8. if ($nums[$i] == $target) {
  9. $start = $i;
  10. break;
  11. }
  12. }
  13. for ($j = $n - 1; $j >= 0; $j--) {
  14. if ($nums[$j] == $target) {
  15. $end = $j;
  16. break;
  17. }
  18. }
  19. return [$start, $end];
  20. }
  21. function searchRange($nums, $target) {
  22. if ($nums == null) return [-1, -1];
  23. $left = 0;
  24. $right = count($nums) - 1;
  25. $result = [-1, -1];
  26. while ($left <= $right) {
  27. $mid = $left + floor(($right - $left) / 2);
  28. if ($nums[$mid] == $target) {
  29. while ($mid >= $left && $nums[$mid] == $target) {
  30. $mid--;
  31. }
  32. $first = $mid + 1;
  33. $mid = $left + floor(($right - $left) / 2);
  34. while ($mid <= $right && $nums[$mid] == $target) {
  35. $mid++;
  36. }
  37. $second = $mid - 1;
  38. $result = [$first, $second];
  39. break;
  40. } else if ($nums[$mid] > $target) {
  41. $right = $mid - 1;
  42. } else {
  43. $left = $mid + 1;
  44. }
  45. }
  46. return $result;
  47. }
  48. function searchRange1($nums, $target) {
  49. if ($nums == null) return [-1, -1];
  50. $left = $this->leftBound($nums, $target);
  51. $right = $this->rightBound($nums, $target);
  52. return [$left, $right];
  53. }
  54. function leftBound($nums, $target) {
  55. $left = 0;
  56. $right = count($nums);
  57. while ($left < $right) {
  58. $mid = $left + floor(($right - $left) / 2);
  59. if ($nums[$mid] == $target) {
  60. $right = $mid;
  61. } else if ($nums[$mid] < $target) {
  62. $left = $mid + 1;
  63. } else if ($nums[$mid] > $targer) {
  64. $right = $mid - 1;
  65. }
  66. if ($left == count($nums)) return -1;
  67. }
  68. return $nums[$left] == $target ? $left : -1;
  69. }
  70. function rightBound($nums, $target) {
  71. $left = 0;
  72. $right = count($nums);
  73. while ($left < $right) {
  74. $mid = $left + floor(($right - $left) / 2);
  75. if ($nums[$mid] == $target) {
  76. $left = $mid + 1;
  77. } else if ($nums[$mid] < $target) {
  78. $left = $mid + 1;
  79. } else if ($nums[$mid] > $targer) {
  80. $right = $mid;
  81. }
  82. }
  83. return $nums[$left - 1] == $target ? ($left - 1) : -1;
  84. }
  85. function searchRange2($nums, $target) {
  86. $l = 0;
  87. $r = count($nums) - 1;
  88. while ($l < $r) {
  89. $mid = $l + floor(($r - $l) / 2);
  90. if ($nums[$mid] >= $target) $r = $mid;
  91. else $l = $mid + 1;
  92. }
  93. if ($nums[$l] != $target) {
  94. return [-1, -1];
  95. } else {
  96. $l = 0;
  97. $r = count($nums) - 1;
  98. while ($l < $r) {
  99. $mid = $l + $r + 1 >> 1;
  100. if ($nums[$mid] <= $target) $l = $mid;
  101. else $r = $mid - 1;
  102. }
  103. return [$l, $l];
  104. }
  105. return [-1, -1];
  106. }
  107. }

GO代码实现:

  1. func searchRange(nums []int, target int) []int {
  2. l := len(nums)
  3. if l == 0 {
  4. return []int{-1, -1}
  5. }
  6. left, right := 0, l-1
  7. a, b := -1, -1
  8. for left <= right {
  9. mid := (left + right) / 2
  10. if nums[mid] == target {
  11. a, b = mid, mid
  12. for a > left && nums[a-1] == target {
  13. a--
  14. }
  15. for b < right && nums[b+1] == target {
  16. b++
  17. }
  18. break
  19. }
  20. if nums[mid] > target {
  21. right = mid - 1
  22. }
  23. if nums[mid] < target {
  24. left = mid + 1
  25. }
  26. }
  27. return []int{a, b}
  28. }