当前位置:网站首页>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];
}
};
边栏推荐
- Anomalies seen during the interview
- Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
- 2022 system integration project management engineer examination knowledge points: software development model
- Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
- Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
- It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
- What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
- Ramble 72 of redis source code
- Version rollback revert don't reset better reset must be forced
- URL (data:image/png; Base64, ivborw0k... Use case
猜你喜欢

Idea set class header comments

Reading notes on how programs run

Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028

A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders

Smart fan system based on stm32f407

Solve the problem that the kaggle account registration does not display the verification code

Briefly understand the operation mode of developing NFT platform

Recommendation of knowledge base management system

Struct in linked list

URL (data:image/png; Base64, ivborw0k... Use case
随机推荐
Anomalies seen during the interview
国元证券开户是真的安全可靠吗
China standard gas market prospect investment and development feasibility study report 2022-2028
For loop
Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
[CSDN Q & A] experience and suggestions
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
挖财帮个人开的证券账户安全吗?是不是有套路
Stock price forecast
Is the securities account opened by Caicai for individuals safe? Is there a routine
P1629 postman delivering letter
Is user authentication really simple
Subgraph isomorphism -subgraph isomorphism
Version rollback revert don't reset better reset must be forced
Several ways to set up a blog locally [attach relevant software download links]
[GNN] hard core! This paper combs the classical graph network model
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Qtcharts notes (V) scatter diagram qscatterseries
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?