当前位置:网站首页>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];
}
}
边栏推荐
- 2022-2028 global military computer industry research and trend analysis report
- es面试题
- [Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
- Basic 01: print string
- Kibana操控ES
- [staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
- Cesium dynamic diffusion point effect
- Set status bar color
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
- Leetcode face T10 (1-9) array, ByteDance interview sharing
猜你喜欢

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding

连通块模板及变式(共4题)

Golang configure export goprivate to pull private library code

Redis cluster
![[JSON] gson use and step on the pit](/img/86/6ee2971715e0d29008a4b7b1a7aa45.jpg)
[JSON] gson use and step on the pit

使用 useDeferredValue 进行异步渲染

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

Jvm-01 (phased learning)

Vsocde has cli every time it is opened js

Share the basic knowledge of a common Hongmeng application
随机推荐
Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
PHP notes - use Smarty to set public pages (include, if, else, variable settings)
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
What are the common proxy servers and what are the differences?
Rotating frame target detection mmrotate v0.3.1 learning model
JVM interview
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
es面试题
Redis cluster
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
Feature query of hypergraph iserver rest Service
Es interview questions
Unit · elementary C # learning notes
Learning notes of software testing -- theoretical knowledge of software testing
C return multiple values getter setter queries the database and adds the list return value to the window
DNS domain name resolution
[JSON] gson use and step on the pit
[pit] how to understand "parameter fishing"
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field