# 在排序数组中查找元素的第一个和最后一个位置

<p>给定一个按照升序排列的整数数组 <code>nums</code>，和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>
<p>如果数组中不存在目标值 <code>target</code>，返回 <code>[-1, -1]</code>。</p>
<p><strong>进阶：</strong></p>
<ul>
    <li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗？</li>
</ul>
<p> </p>
<p><strong>示例 1：</strong></p>
<pre><strong>输入：</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出：</strong>[3,4]</pre>
<p><strong>示例 2：</strong></p>
<pre><strong>输入：</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出：</strong>[-1,-1]</pre>
<p><strong>示例 3：</strong></p>
<pre><strong>输入：</strong>nums = [], target = 0<strong><br />输出：</strong>[-1,-1]</pre>
<p> </p>
<p><strong>提示：</strong></p>
<ul>
    <li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
    <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
    <li><code>nums</code> 是一个非递减数组</li>
    <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
</ul>

## template

```java
public class Solution {

	public int[] searchRange(int[] nums, int target) {
		int[] result = new int[2];
		result[0] = floor(nums, target);
		result[1] = ceil(nums, target);
		return result;
	}

	private int floor(int[] nums, int target) {
		int left = -1;
		int right = nums.length - 1;
		while (left < right) {
			int mid = left + (right - left + 1) / 2;
			if (target <= nums[mid]) {
				right = mid - 1;
			} else {
				left = mid;
			}
		}
		if (left + 1 < nums.length && nums[left + 1] == target) {
			return left + 1;
		} else {
			return -1;
		}
	}

	private int ceil(int[] nums, int target) {
		int left = 0;
		int right = nums.length;
		while (left < right) {
			int mid = left + (right - left) / 2;
			if (target >= nums[mid]) {
				left = mid + 1;
			} else {
				right = mid;
			}
		}
		if (right - 1 >= 0 && nums[right - 1] == target) {
			return right - 1;
		} else {
			return -1;
		}
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```