当前位置:网站首页>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];
}
}
边栏推荐
- The number one malware in January 2022: lokibot returned to the list, and emotet returned to the top
- Software testing learning notes - network knowledge
- LFM signal denoising, time-frequency analysis, filtering
- What are the common proxy servers and what are the differences?
- Kibana controls es
- Questions d'entrevue
- Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market
- New programmer magazine | Li Penghui talks about open source cloud native message flow system
- [Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
- 【做题打卡】集成每日5题分享(第二期)
猜你喜欢
LeetCode刷题(十)——顺序刷题46至50
query词权重, 搜索词权重计算
Leetcode face T10 (1-9) array, ByteDance interview sharing
After marriage
el-table的render-header用法
Golang configure export goprivate to pull private library code
Comparative analysis of MVC, MVP and MVVM, source code analysis
2022-2028 global wood vacuum coating machine industry research and trend analysis report
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
随机推荐
LFM信号加噪、时频分析、滤波
The basic steps of using information theory to deal with scientific problems are
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
多线程查询,效率翻倍
实现一个自定义布局的扫码功能
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Actual battle of financial risk control - under Feature Engineering
es面试题
QT uses sqllite
Remote connection to MySQL under windows and Linux system
Start from scratch - Web Host - 01
离婚3年以发现尚未分割的共同财产,还可以要么
Batch detect whether there is CDN in URL - high accuracy
[pit] how to understand "parameter fishing"
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
2022-2028 global wood vacuum coating machine industry research and trend analysis report
2022-2028 global encryption software industry research and trend analysis report
Divorce for 3 years to discover the undivided joint property, or