当前位置:网站首页>A single element in an ordered array
A single element in an ordered array
2022-07-01 00:33:00 【Xiao Qi,.】
An ordered array of integers only , Each of these elements will appear twice , Only one number will appear only once , Design an algorithm to find this number and return .
for example :
Input :[ 1,1,2,2,3,3,4,5,5 ]
Output :4
Input :[ 3,3,4,4,5,5,6 ]
Output :6
Except that a number only appears once , Every other number will appear twice , Note that this ordered array must contain an odd number of numbers , And the subscript of a number that only appears once must be an even number . And the array is ordered , You can use binary search to get results .
int singleNonDuplicate(int* nums, int numsSize) {
int left=0;
int right=numsSize-1;
while(low<high){
int mid=left+(right-left)/2;// Two points search
mid-=mid&1;// Odd number &1 by 1, even numbers &1 by 0
if(nums[mid]==nums[mid+1]){// If equal, it means
left+=2;// Traverse only numbers with even Subscripts
}
else right=mid;
}
return nums[left];
}边栏推荐
- Quick start of wechat applet -- project introduction
- Error 2059 when Navicat connects to MySQL
- MySQL variables, stored procedures and functions
- 20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
- What SQL statements are supported for data filtering
- Don't worry about whether you can be a coder if you don't learn English well. Learn it first
- Basic data structure of redis
- leetcode 474. Ones and zeroes (medium)
- 优质的水泵 SolidWorks模型素材推荐,不容错过
- 1009 product of polynomials (25 points) [PTA class A]
猜你喜欢

When is it appropriate to replace a virtual machine with a virtual machine?

BeanUtils. Copyproperties() vs. mapstruct

The programmer's girlfriend gave me a fatigue driving test

Wechat official account development (1) introduction to wechat official account

Teach you how to use Hal library to get started -- become a lighting master

How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?

Why did kubernetes win? The changes in the container circle!

2022-2028 global 3D printing ASA consumables industry research and trend analysis report
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
随机推荐
Query points in MATLAB Delaunay triangulation
Unit test concept and purpose
Bridge emqx cloud data to AWS IOT through the public network
Combining online and offline, VR panorama is a good way to transform furniture online!
Tide - rust web framework based on async STD
$watch will not trigger data change - $watch not firing on data change
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
Design e-commerce seckill system
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
SSM integration process (integration configuration, function module development, interface test)
How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?
Quick start of wechat applet -- project introduction
Rhai - rust's embedded scripting engine
优质的水泵 SolidWorks模型素材推荐,不容错过
2022-2028 global ICT test probe industry research and trend analysis report
Don't worry about whether you can be a coder if you don't learn English well. Learn it first
On the application of cluster analysis in work
[designmode] singleton pattern
Confirm() method of window