当前位置:网站首页>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];
}
};
边栏推荐
- Unity elementary case notes of angry birds Siki college 1-6
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Advanced C language - pointer 2 - knowledge points sorting
- Is the securities account opened by Caicai for individuals safe? Is there a routine
- [C language] break and continue in switch statement
- 网上的低佣金链接安全吗?招商证券怎么开户?
- Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
- Analysis: misunderstanding of choosing WMS warehouse management system
- How to be a professional software testing engineer? Listen to the byte five year old test
猜你喜欢

Vscode regular match replace console log(.*)

MySQL winter vacation self-study 2022 12 (1)

China standard gas market prospect investment and development feasibility study report 2022-2028

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

leetcode-43. String multiplication

The difference between objects and objects

Idea integrates Microsoft TFs plug-in

(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/28/fc05f2e8d53cf81fd061c799090022.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/3f/75b3125f8779e6cf9467a30fd7eeb4.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
随机推荐
[CSDN Q & A] experience and suggestions
国元证券开户是真的安全可靠吗
Struct in linked list
Gossip about redis source code 79
Is user authentication really simple
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
What does redis do? Redis often practices grammar every day
Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
Zipper table in data warehouse (compressed storage)
HR disgusted interview behavior
想请教一下,十大劵商如何开户?在线开户是安全么?
2020.2.14
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
What is the potential of pocket network, which is favored by well-known investors?
【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
Gossip about redis source code 80
Joint examination of six provinces 2017
P1656 bombing Railway
Distributed transaction -- middleware of TCC -- selection / comparison
[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept