当前位置:网站首页>Force deduction daily question 540 A single element in an ordered array
Force deduction daily question 540 A single element in an ordered array
2022-07-02 02:51:00 【Comma 8080】
540. A single element in an ordered array
Give you an ordered array of integers only , Each of these elements will appear twice , Only one number will appear once .
Please find and return the number that only appears once .
The solution you design must meet O(log n)
Time complexity and O(1)
Spatial complexity .
Example 1:
Input : nums = [1,1,2,3,3,4,4,8,8]
Output : 2
Example 2:
Input : nums = [3,3,7,7,10,11,11]
Output : 10
Tips :
1 <= nums.length <= 105
0 <= nums[i] <= 105
Answer key
This problem can directly use bit operation for XOR , But the topic requirements are met O(log n)
Time complexity and O(1)
Spatial complexity .
So choose dichotomy to solve
We can find that what we are given is an ordered array , From this we can get , If an element appears twice , Then these two times are adjacent
So we take the middle number , Then judge the numbers on both sides of him. If he is different from the numbers on both sides , Then this number is the number that only appears once
At the same time, we can also know two conditions
If the subscript of the middle number is even ( Judge by array subscript ), And this number is equal to the number at the left end , The number that only appears once must be on his left , And vice versa
If it's odd , And this number is equal to the number at the left end , The number that only appears once must be at his right end , And vice versa
Code
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
int l = 0, r = n - 1;
while(l < r){
int mid = l + r >> 1;
if(mid % 2 == 0){
if(mid + 1 < n && nums[mid] == nums[mid + 1]){
l = mid + 1;
}else{
r = mid;
}
}else{
if(mid - 1 >= 0 && nums[mid] == nums[mid - 1]){
l = mid + 1;
}else{
r = mid;
}
}
}
return nums[r];
}
}
边栏推荐
- 2022-2028 global wood vacuum coating machine industry research and trend analysis report
- Vsocde has cli every time it is opened js
- Après le mariage
- The number one malware in January 2022: lokibot returned to the list, and emotet returned to the top
- 【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
- Mongodb non relational database
- tarjan2
- New programmer magazine | Li Penghui talks about open source cloud native message flow system
- 2022-2028 global military computer industry research and trend analysis report
- Deployment practice and problem solving of dash application development environment based on jupyter Lab
猜你喜欢
[opencv] - comprehensive examples of five image filters
結婚後
Vsocde has cli every time it is opened js
How to turn off the LED light of Rog motherboard
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
MongoDB非關系型數據庫
Pat a-1165 block reversing (25 points)
Use usedeferredvalue for asynchronous rendering
Feature query of hypergraph iserver rest Service
批量检测url是否存在cdn—高准确率
随机推荐
Provincial election + noi Part IV graph theory
Software testing learning notes - network knowledge
Yyds dry goods inventory accelerating vacuum in PG
Additional: information desensitization;
[JSON] gson use and step on the pit
Kibana controls es
C write TXT file
Stdref and stdcref
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
essay structure
自定义组件的 v-model
Remote connection to MySQL under windows and Linux system
Mongodb non relational database
2022安全员-C证考试题及模拟考试
The number one malware in January 2022: lokibot returned to the list, and emotet returned to the top
JVM interview
Baohong industry | 6 financial management models at different stages of life
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
QT实现界面跳转
2022-2028 global military computer industry research and trend analysis report