当前位置:网站首页>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 <= 1050 <= 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];
}
}
边栏推荐
- How to turn off the LED light of Rog motherboard
- Remote connection to MySQL under windows and Linux system
- Realize the code scanning function of a custom layout
- Questions d'entrevue
- PHP notes - use Smarty to set public pages (include, if, else, variable settings)
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- Systemserver service and servicemanager service analysis
- Leetcode question brushing (10) - sequential question brushing 46 to 50
- Provincial election + noi Part IV graph theory
- What kind of good and cost-effective Bluetooth sports headset to buy
猜你喜欢

Render header usage of El table
![[reading notes] programmer training manual - practical learning is the most effective (project driven)](/img/13/28116a74512895ad725dffed02f8bd.png)
[reading notes] programmer training manual - practical learning is the most effective (project driven)

How to run oddish successfully from 0?

MongoDB非關系型數據庫

2022-2028 global encryption software industry research and trend analysis report

【liuyubobobo-玩转Leetcode算法面试】【00】课程概述

Which kind of sports headphones is easier to use? The most recommended sports headphones
![[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)](/img/39/42b1726e5f446f126a42d7ac673dce.png)
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)

Which brand of sports headset is better? Bluetooth headset suitable for sports

Batch detect whether there is CDN in URL - high accuracy
随机推荐
What kind of good and cost-effective Bluetooth sports headset to buy
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
Multi threaded query, double efficiency
Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
Unit · elementary C # learning notes
Share the basic knowledge of a common Hongmeng application
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
C return multiple values getter setter queries the database and adds the list return value to the window
MMSegmentation系列之训练与推理自己的数据集(三)
The number one malware in January 2022: lokibot returned to the list, and emotet returned to the top
[pit] how to understand "parameter fishing"
New programmer magazine | Li Penghui talks about open source cloud native message flow system
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
What is the function of the headphone driver
[road of system analyst] collection of wrong topics in enterprise informatization chapter
旋转框目标检测mmrotate v0.3.1 学习模型
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
Websocket + spingboot realize code scanning login