Skip to content

Commit f058e9c

Browse files
authored
Merge pull request #613 from LetMeFly666/540
添加问题“540.有序数组中的单一元素”的代码和题解
2 parents 3d8b84a + 4a00a1b commit f058e9c

6 files changed

+250
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-10 16:42:30
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-10 17:28:09
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
#ifdef FirstTry
12+
class Solution {
13+
public:
14+
int singleNonDuplicate(vector<int>& nums) {
15+
int l = 0, r = nums.size();
16+
while (l < r) {
17+
int mid = (l + r) >> 1;
18+
if (mid == nums.size() - 1 || nums[mid] != nums[mid ^ 1]) { // 问题出在mid及其之前
19+
if ((mid == 0 || nums[mid] != nums[mid - 1]) && (mid == nums.size() - 1 || nums[mid] != nums[mid + 1])) {
20+
return nums[mid];
21+
}
22+
r = mid;
23+
} else {
24+
l = mid + 1;
25+
}
26+
}
27+
// printf("l = %d\n", l); //************
28+
return nums[l];
29+
}
30+
};
31+
#else // FirstTry
32+
// SecondTry
33+
class Solution {
34+
public:
35+
int singleNonDuplicate(vector<int>& nums) {
36+
int l = 0, r = nums.size() - 1;
37+
while (l < r) {
38+
int mid = (l + r) >> 1;
39+
if (nums[mid] == nums[mid ^ 1]) {
40+
l = mid + 1;
41+
} else {
42+
r = mid;
43+
}
44+
}
45+
return nums[l];
46+
}
47+
};
48+
#endif // FirstTry
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-10 16:45:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-10 17:41:04
6+
*/
7+
package main
8+
9+
func singleNonDuplicate(nums []int) int {
10+
l, r := 0, len(nums) - 1
11+
for l < r {
12+
mid := (l + r) >> 1
13+
if nums[mid] == nums[mid + 1] {
14+
l = mid + 1
15+
} else {
16+
r = mid
17+
}
18+
}
19+
return nums[l]
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-10 16:45:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-10 17:49:31
6+
*/
7+
class Solution {
8+
public int singleNonDuplicate(int[] nums) {
9+
int l = 0, r = nums.length - 1;
10+
while (l < r) {
11+
int mid = (l + r) >> 1;
12+
if (nums[mid] == nums[mid ^ 1]) {
13+
l = mid + 1;
14+
} else {
15+
r = mid;
16+
}
17+
}
18+
return nums[l];
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-10 16:44:54
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-10 17:38:12
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def singleNonDuplicate(self, nums: List[int]) -> int:
11+
l, r = 0, len(nums) - 1
12+
while l < r:
13+
mid = (l + r) >> 1
14+
if nums[mid] == nums[mid ^ 1]:
15+
l = mid + 1
16+
else:
17+
r = mid
18+
return nums[l]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
|0529.扫雷游戏|中等|<a href="https://leetcode.cn/problems/minesweeper/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/23/LeetCode%200529.%E6%89%AB%E9%9B%B7%E6%B8%B8%E6%88%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127997191" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minesweeper/solutions/1986830/letmefly-529sao-lei-you-xi-by-tisfy-cffc/" target="_blank">LeetCode题解</a>|
206206
|0530.二叉搜索树的最小绝对差|简单|<a href="https://leetcode.cn/problems/minimum-absolute-difference-in-bst/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/23/LeetCode%200530.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127997530" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-absolute-difference-in-bst/solutions/1986865/letmefly-530er-cha-sou-suo-shu-de-zui-xi-716i/" target="_blank">LeetCode题解</a>|
207207
|0538.把二叉搜索树转换为累加树|中等|<a href="https://leetcode.cn/problems/convert-bst-to-greater-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/04/LeetCode%200538.%E6%8A%8A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%BD%AC%E6%8D%A2%E4%B8%BA%E7%B4%AF%E5%8A%A0%E6%A0%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128174296" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-bst-to-greater-tree/solutions/2005661/letmefly-538ba-er-cha-sou-suo-shu-zhuan-vivvg/" target="_blank">LeetCode题解</a>|
208+
|0540.有序数组中的单一元素|中等|<a href="https://leetcode.cn/problems/single-element-in-a-sorted-array/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/10/LeetCode%200540.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%8D%95%E4%B8%80%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143663976" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/single-element-in-a-sorted-array/solutions/2983931/letmefly-540you-xu-shu-zu-zhong-de-dan-y-uhdj/" target="_blank">LeetCode题解</a>|
208209
|0542.01矩阵|中等|<a href="https://leetcode.cn/problems/01-matrix/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/04/LeetCode%200542.01%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128175163" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/01-matrix/solutions/2005765/letmefly-54201-ju-zhen-by-tisfy-cycy/" target="_blank">LeetCode题解</a>|
209210
|0543.二叉树的直径|简单|<a href="https://leetcode.cn/problems/diameter-of-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/05/LeetCode%200543.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%9B%B4%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128194953" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/diameter-of-binary-tree/solutions/2007705/letmefly-543er-cha-shu-de-zhi-jing-by-ti-k7o9/" target="_blank">LeetCode题解</a>|
210211
|0547.省份数量|中等|<a href="https://leetcode.cn/problems/number-of-provinces/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/13/LeetCode%200547.%E7%9C%81%E4%BB%BD%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128304781" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-provinces/solutions/2018995/letmefly-547sheng-fen-shu-liang-by-tisfy-nykg/" target="_blank">LeetCode题解</a>|
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: 540.有序数组中的单一元素
3+
date: 2024-11-10 17:53:24
4+
tags: [题解, LeetCode, 中等, 数组, 二分查找, 位运算]
5+
---
6+
7+
# 【LetMeFly】540.有序数组中的单一元素:二分查找(位运算优化)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/single-element-in-a-sorted-array/](https://leetcode.cn/problems/single-element-in-a-sorted-array/)
10+
11+
<p>给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。</p>
12+
13+
<p>请你找出并返回只出现一次的那个数。</p>
14+
15+
<p>你设计的解决方案必须满足 <code>O(log n)</code> 时间复杂度和 <code>O(1)</code> 空间复杂度。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong> nums = [1,1,2,3,3,4,4,8,8]
23+
<strong>输出:</strong> 2
24+
</pre>
25+
26+
<p><strong>示例 2:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong> nums = [3,3,7,7,10,11,11]
30+
<strong>输出:</strong> 10
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><meta charset="UTF-8" /></p>
36+
37+
<p><strong>提示:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>0 &lt;= nums[i]&nbsp;&lt;= 10<sup>5</sup></code></li>
42+
</ul>
43+
44+
45+
46+
## 解题方法:二分查找
47+
48+
数组元素有序说明,我们可以直接随机选择一个下标,如果“当前下标为偶数且当前元素和下一个元素相同”或者“当前下标为奇数并且当前元素和上一个元素相同”,则说明从头开始到这个元素为止每个元素都是成对出现的。
49+
50+
因此我们可以直接进行二分操作:每次枚举mid并在$O(1)$的时间内得到$[0, mid]$中的每个元素是否都成对出现。若成对出现则说明答案在$mid + 1$及之后;否则说明答案在$mid$及之前。
51+
52+
位运算优化:
53+
54+
1. $\frac{l+r}2=(l+r)>>1$
55+
2. 如果$mid$是奇数,那么应该判断$nums[mid]$是否和$nums[mid - 1]$相等;如果$mid$是偶数,那么应该判断$nums[mid]$是否和$nums[mid + 1]$相等。总之,我们只需要判断$nums[mid]$和$nums[mid \hat\ 1]$是否相等(其中$\hat\ $是异或符)
56+
57+
(・∀・(・∀・(・∀・*)
58+
59+
+ 时间复杂度$O(\log len(nums))$
60+
+ 空间复杂度$O(1)$
61+
62+
### AC代码
63+
64+
#### C++
65+
66+
```cpp
67+
class Solution {
68+
public:
69+
int singleNonDuplicate(vector<int>& nums) {
70+
int l = 0, r = nums.size() - 1;
71+
while (l < r) {
72+
int mid = (l + r) >> 1;
73+
if (nums[mid] == nums[mid ^ 1]) {
74+
l = mid + 1;
75+
} else {
76+
r = mid;
77+
}
78+
}
79+
return nums[l];
80+
}
81+
};
82+
```
83+
84+
#### Python
85+
86+
```python
87+
from typing import List
88+
89+
class Solution:
90+
def singleNonDuplicate(self, nums: List[int]) -> int:
91+
l, r = 0, len(nums) - 1
92+
while l < r:
93+
mid = (l + r) >> 1
94+
if nums[mid] == nums[mid ^ 1]:
95+
l = mid + 1
96+
else:
97+
r = mid
98+
return nums[l]
99+
```
100+
101+
#### Java
102+
103+
```java
104+
class Solution {
105+
public int singleNonDuplicate(int[] nums) {
106+
int l = 0, r = nums.length - 1;
107+
while (l < r) {
108+
int mid = (l + r) >> 1;
109+
if (nums[mid] == nums[mid ^ 1]) {
110+
l = mid + 1;
111+
} else {
112+
r = mid;
113+
}
114+
}
115+
return nums[l];
116+
}
117+
}
118+
```
119+
120+
#### Go
121+
122+
```go
123+
package main
124+
125+
func singleNonDuplicate(nums []int) int {
126+
l, r := 0, len(nums) - 1
127+
for l < r {
128+
mid := (l + r) >> 1
129+
if nums[mid] == nums[mid + 1] {
130+
l = mid + 1
131+
} else {
132+
r = mid
133+
}
134+
}
135+
return nums[l]
136+
}
137+
```
138+
139+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/10/LeetCode%200540.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%8D%95%E4%B8%80%E5%85%83%E7%B4%A0/)哦~
140+
>
141+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/143663976](https://letmefly.blog.csdn.net/article/details/143663976)
142+
143+
emm,某事还是很累,这篇题解写地晕哩糊涂的。

0 commit comments

Comments
 (0)