当前位置:网站首页>A single element in an ordered array -- Valentine's Day mental problems
A single element in an ordered array -- Valentine's Day mental problems
2022-07-02 23:29:00 【IOT classmate Huang】
A single element in an ordered array ——Leetcode Valentine's Day is about mentality
subject
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 : 10Tips :
1 <= nums.length <= 1050 <= nums[i] <= 105Attach the title link :540. A single element in an ordered array - Power button (LeetCode) (leetcode-cn.com)
Personal feelings
yysy, After I understand the topic , I really feel like I've been caught , I admire leetcode The author , Among thousands of questions, cold food was found for him to kill , Valentine's Day is devoted to my mentality , How come everyone else is in pairs, right , Man, I'm unique , Put it here and hit the keyboard, right . Scald !
Answer key
There's nothing to say , Two point search to judge the odd and even subscript is over
Because there is only one unique and ordered , So we can assume that the subscript of the unique element is x, Its left subscript y, If nums[y] = nums[y+1], be y For the even , alike , For the right , If nums[y] = nums[y+1], be y It's odd .
Based on this , You can set binary search .
I am here leetcode The antithesis of , You can have a look at what you need Two points search - A single element in an ordered array - Power button (LeetCode) (leetcode-cn.com)
code( Full version )
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution
{
public:
/// <summary>
/// Two points search
/// Because there is only one unique and ordered
/// So suppose the only subscript is x, Its left subscript y, If nums[y] = nums[y+1], be y For the even
/// alike , For the right , If nums[y] = nums[y+1], be y It's odd
/// Set up a two-point search according to the appeal
/// At the beginning , The boundary on the left is 0, The boundary on the right is n-1,n Is the number of array elements
/// Every time I look up mid Take the middle of the left boundary and the right boundary , Divide parity judgment
/// When mid In an odd number of , Judge nums[mid] == nums[mid-1]
/// When mid For even when , Judge nums[mid] == nums[mid+1]
/// The above set up a mid < x, conversely mid > x
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
int singleNonDuplicate(vector<int>& nums)
{
int n = nums.size();
// A special case , There is only one number
if (n == 1)
{
return nums[0];
}
int left = 0, right = n - 1;
// Two points search
while (left < right)
{
int mid = left + (right - left) / 2;
if (mid % 2 == 0 && nums[mid] == nums[mid + 1] ||
mid % 2 == 1 && nums[mid] == nums[mid - 1])
{
left = mid + 1;
}
else
{
right = mid;
}
}
return nums[left];
}
};
int main(int argc, char** argv)
{
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; ++i)
{
cin >> nums[i];
}
// Make sure the data you enter is in order
sort(nums.begin(), nums.end());
Solution sol;
cout << sol.singleNonDuplicate(nums) << endl;
return 0;
}
The latter
Today is the day of being killed ...
边栏推荐
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- SQL进阶语法
- 提交代码流程
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- Introduction to the latest plan of horizon in April 2022
- "A good programmer is worth five ordinary programmers!"
- [redis notes] compressed list (ziplist)
- @BindsInstance在Dagger2中怎么使用
- Call vs2015 with MATLAB to compile vs Project
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
猜你喜欢

How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!

公司里只有一个测试是什么体验?听听他们怎么说吧
![[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)](/img/a3/d8421ea1539eba08bf7a5a629d92e6.jpg)
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)

抖音实战~点赞数量弹框

Application of containerization technology in embedded field

"A good programmer is worth five ordinary programmers!"
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

【Redis笔记】压缩列表(ziplist)

Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11

非路由组件之头部组件和底部组件书写
随机推荐
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Win11如何开启目视控制?Win11开启目视控制的方法
Yolox enhanced feature extraction network panet analysis
Doorplate making C language
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
面试过了,起薪16k
JSON数据传递参数
公司里只有一个测试是什么体验?听听他们怎么说吧
详解Promise使用
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
跨境电商如何通过打好数据底座,实现低成本稳步增长
Writing of head and bottom components of non routing components
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
VIM interval deletion note
Fusion de la conversion et de la normalisation des lots
RuntimeError: no valid convolution algorithms available in CuDNN
基于FPGA的VGA协议实现
MarkDown基本语法
程序员版本的八荣八耻~
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)