当前位置:网站首页>A dichotomy of Valentine's Day
A dichotomy of Valentine's Day
2022-07-04 00:26:00 【Apprentice of Illusionist】
Title Description
A single element in an ordered array 
Topic ideas
The key to this question is to realize that before individual elements appear , Because arrays are ordered , Two identical elements are arranged together , Observe the subscript to find :
1 1 2 2 3 4 4
0 1 2 3 4 5 6
n u m s [ y ] = = n u m s [ y + 1 ] ⇔ y yes accidentally Count n u m s [ y − 1 ] = = n u m s [ y ] ⇔ y yes p. Count nums[y] == nums[y + 1] \Leftrightarrow y It's even \\ nums[y - 1] == nums[y] \Leftrightarrow y Is odd \\ nums[y]==nums[y+1]⇔y yes accidentally Count nums[y−1]==nums[y]⇔y yes p. Count
When individual elements appear , Yes :
n u m s [ z ] = = n u m s [ z + 1 ] ⇔ z yes p. Count n u m s [ z − 1 ] = = n u m s [ z ] ⇔ z yes accidentally Count nums[z] == nums[z + 1] \Leftrightarrow z Is odd \\ nums[z - 1] == nums[z] \Leftrightarrow z It's even \\ nums[z]==nums[z+1]⇔z yes p. Count nums[z−1]==nums[z]⇔z yes accidentally Count
So we can use this property to dichotomy , Find the right endpoint satisfying the first property interval or the left endpoint satisfying the second property interval .
class Solution {
public:
int singleNonDuplicate(vector<int>& nums)
{
int n = nums.size();
int l = 0, r = n - 1;
while (l < r)
{
/* Because the update is l = mid So we need to add 1*/
int mid = (l + r + 1) >> 1;
if ((mid & 1) == 0)
{
if (mid > 0 && nums[mid - 1] == nums[mid])
r = mid - 1;
else l = mid;
}
else
{
if (mid + 1 < n && nums[mid] == nums[mid + 1])
r = mid - 1;
else l = mid;
}
}
return nums[l];
}
};
This problem can be further optimized , Think about a bit operation Exclusive or , if x Is odd , be x ^ 1 = x - 1, if x It's even , be x ^ 1 = x, The two cases can be summed up by using XOR :
class Solution {
public:
int singleNonDuplicate(vector<int>& nums)
{
// The key to this question lies in the single element x Before appearance
// if nums[y] == nums[y + 1] be y Must be an even number
// In a single element x After a
// if nums[z] == nums[z + 1] be z It must be odd
// You can use this property to dichotomy
// further When mid It's an odd number mid ^ 1 = mid - 1
// When mid When it's even mid ^ 1 = mid + 1
// We find satisfaction nums[y] == nums[y + 1] <-> y For the even
//nums[y - 1] == nums[y] <-> y Is odd
// The right border of
// So we just need to compare nums[mid] == nums[mid ^ 1] Whether it is established or not There is no need to discuss parity
int l = 0, r = nums.size() - 1;
while (l < r)
{
int mid = (l + r) >> 1;
/* If this property is satisfied shows mid Still in the left section In order to find the boundary Give Way l = mid + 1*/
/* There will be no cross-border Because if mid be equal to 0 Exclusive or 1 still 0 And mid = (l + r) / 2 <= r mid + 1 Greater than r Will not be greater than n - 1 */
if (nums[mid] == nums[mid ^ 1]) l = mid + 1;
/* Otherwise, explain mid Not in the left section to update r by mid*/
else r = mid;
}
return nums[l];
}
};
You can also observe :
1 1 2 2 3 4 4
0 1 2 3 4 5 6
Because the left side of a single number is a pair of identical numbers , So the subscript of the number appearing alone must be even , Even subscripts can be bisected , keep mid The condition of being even is mid -= mid & 1,mid It's an odd number ,mid & 1 yes 1;mid When it's even ,mid & 1 yes 0.
The first unsatisfied property nums[y] == nums[y + 1] The even subscript of is the answer .
class Solution {
public:
int singleNonDuplicate(vector<int>& nums)
{
int n = nums.size();
int l = 0, r = n - 1;
while (l < r)
{
int mid = (l + r) >> 1;
mid -= (mid & 1);
/* If the conditions are met Then let l Two steps forward */
if (mid + 1 < n && nums[mid] == nums[mid + 1])
l = mid + 2;
/* If not satisfied Give Way r = mid Control must be able to scan the answer */
else r = mid;
}
return nums[r];
}
};
边栏推荐
- Subgraph isomorphism -subgraph isomorphism
- Idea integrates Microsoft TFs plug-in
- CSP window
- [MySQL] classification of multi table queries
- Selenium library 4.5.0 keyword explanation (II)
- Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
- Gossip about redis source code 81
- Unity elementary case notes of angry birds Siki college 1-6
- Alibaba cloud container service differentiation SLO hybrid technology practice
- Generic tips
猜你喜欢
![[PHP basics] cookie basics, application case code and attack and defense](/img/7c/551b79fd5dd8a411de85c800c3a034.jpg)
[PHP basics] cookie basics, application case code and attack and defense

MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query

Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition

Interview script of Software Test Engineer

Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival

Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn

Recommendation of knowledge base management system

Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
![[C language] break and continue in switch statement](/img/ae/5967fefcf3262c9d3096e5c7d644fd.jpg)
[C language] break and continue in switch statement

Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
随机推荐
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
A method to solve Bert long text matching
Anomalies seen during the interview
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
ISBN number
MySQL is installed as a Windows Service
Correlation analysis summary
AI Challenger 2018 text mining competition related solutions and code summary
[Mongodb] 2. Use mongodb --------- use compass
No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
How to trade spot gold safely?
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
Alibaba cloud container service differentiation SLO hybrid technology practice
Optimization of for loop
After the Lunar New Year and a half
【leetcode】374. Guess the size of the number
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
SPI based on firmware library