当前位置:网站首页>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];
}
};
边栏推荐
- ITK learning notes (VII) the position of ITK rotation direction remains unchanged
- Is the securities account opened by Caicai for individuals safe? Is there a routine
- Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
- [MySQL] sql99 syntax to realize multi table query
- Social network analysis -social network analysis
- Detailed explanation of the relationship between Zhongtai, wechat and DDD
- What are the application fields of digital twins in industry?
- After the Lunar New Year and a half
- Gossip about redis source code 77
- 挖财帮个人开的证券账户安全吗?是不是有套路
猜你喜欢
What is the Valentine's Day gift given by the operator to the product?
Idea integrates Microsoft TFs plug-in
Correlation analysis summary
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
JDBC Technology
MySQL is installed as a Windows Service
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Test the influence of influent swacth on the electromagnetic coil of quartz meter
Tencent interview: can you pour water?
MySQL winter vacation self-study 2022 12 (1)
随机推荐
Recommendation of knowledge base management system
Investment demand and income forecast report of China's building ceramics industry, 2022-2028
[MySQL] sql99 syntax to realize multi table query
2022 Software Test Engineer skill list, please check
URL (data:image/png; Base64, ivborw0k... Use case
Data storage - interview questions
China standard gas market prospect investment and development feasibility study report 2022-2028
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
2022 system integration project management engineer examination knowledge points: software development model
Test the influence of influent swacth on the electromagnetic coil of quartz meter
Suggestions for improving code quality
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Zipper table in data warehouse (compressed storage)
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
Pytorch learning notes 5: model creation
2020.2.14
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
Interview script of Software Test Engineer
Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
Att & CK actual combat series - red team actual combat - V