劍指 Offer II 070. 排序數(shù)組中只出現(xiàn)一次的數(shù)字
### LeetCode [劍指 Offer II 070. 排序數(shù)組中只出現(xiàn)一次的數(shù)字](https://leetcode.cn/problems/skFtm2/)
@[TOC](文章目錄)
#### 題目描述
> 給定一個(gè)只包含整數(shù)的有序數(shù)組 nums ,每個(gè)元素都會出現(xiàn)兩次,唯有一個(gè)數(shù)只會出現(xiàn)一次,請找出這個(gè)唯一的數(shù)字。
>
>你設(shè)計(jì)的解決方案必須滿足 O(log n) 時(shí)間復(fù)雜度和 O(1) 空間復(fù)雜度。
示例:
```
輸入: nums = [1,1,2,3,3,4,4,8,8]
輸出: 2
```
提示:
```
1 <= nums.length <= 105
0 <= nums[i] <= 105
```
#### 一、解題關(guān)鍵詞
```
有序
出現(xiàn)兩次
找到出現(xiàn)一次的數(shù)字
滿足logn 復(fù)雜度
```
#### 二、解題報(bào)告
#### 1.思路分析
1. 因?yàn)橛行?,所以先考慮二分,
2. 二分沒有具體的判別標(biāo)準(zhǔn),所以需要所有元素遍歷,累加出現(xiàn)次數(shù)
3. 一個(gè)循環(huán)累加
4. 一個(gè)循環(huán)判斷
#### 2.時(shí)間復(fù)雜度
#### 3.代碼示例
```java
class Solution {
? ? public int singleNonDuplicate(int[] nums) {
? ? ? ? int len = nums.length;
? ? ? ? Map<Integer,Integer> map = new HashMap<>();
? ? ? ? for(int i = 0; i < len; i++){
? ? ? ? ? ? map.put(nums[i],map.getOrDefault(nums[i],0) + 1);
? ? ? ? }
? ? ? ? for(int i = 0; i < len; i++){
? ? ? ? ? ? if(map.get(nums[i]) == 1){
? ? ? ? ? ? ? ? return nums[i];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return -1;
? ? }
}
```
#### 4.知識點(diǎn)
```
```
---
#### 三、思考
##### 1.相同題目
[540. 有序數(shù)組中的單一元素](https://leetcode.cn/problems/single-element-in-a-sorted-array/)