当前位置:网站首页>Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself
Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself
2022-07-03 20:56:00 【Ape Xiaofu】
Content of this article :leetcode A daily topic 540. A single element in an ordered array Valentine's Day Special A bunch of couples looking for a single dog ~ I didn't expect that the clown was myself
Article column :leetcode A daily topic 《 Punch in daily 》
Recent updates :2022 year 2 month 13 Japan leetcode A daily topic 1189. “ balloon ” Maximum number of Simple simulation questions ~
Personal profile : A Junior Program ape in two colleges , In the spirit of paying attention to the foundation , Clock in algorithm , Sharing technology as a personal experience summary blogger , Although you may be lazy sometimes , But I will stick to it , If you like blog posts very much , Suggest looking at the following line ~( Crazy hints QwQ)
give the thumbs-up Collection Leaving a message. One key, three links Care program ape , From you and me
Contents of this article
Write it at the front
Today is Valentine's Day however The profound meaning of this question makes the brain buzzing , Pair up , Find the elements listed , Good guy, break the defense directly , Find a single dog among lovers , I didn't think! , The clown is myself ~
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
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 : 10
Tips
1 <= nums.length <= 10^50 <= nums[i] <= 10^5
Ideas
This question examines the knowledge points
There are roughly two ways to solve problems :
- The first one is : Direct violence AC Because this is an ordered array , If from the first group of elements ( A set of elements is 2 individual ) Start , If a group of elements are different Then there must be a single element between the two . Of course, you can also use hash table counting .
- The second kind : Because the time complexity of this problem is expected to be logn Then we can't use violence , Pairing , Two points search .
Code implementation
violence AC
class Solution {
public int singleNonDuplicate(int[] nums) {
int n= nums.length;
// Pair up If there is inconsistency in this group, it is not a couple Returns the first element
for(int i=0;i<n-1;i+=2){
if(nums[i] != nums[i+1]){
return nums[i];
}
}
// If you traverse to the last element, you will directly return this single dog
return nums[n-1];
}
}
Two points ( Valentine's Day Is dichotomy really good ?)
class Solution {
public int singleNonDuplicate(int[] nums) {
int left = 0;
int right = nums.length-1;
int mid ;
// Just set the template directly
while(left < right){
mid = left + (right - left )/2;
if (mid % 2 == 1)mid--;
if (nums[mid] == nums[mid+1])
left+=2;
else {
right = mid;
}
}
return nums[left];
}
}
Running results
violence AC

Two points search 
At the end
2022-2-14 Xiao Fu clocked in today ~
A beautiful sunrise Beautiful mountains and rivers
Because of you And bright dazzling

边栏推荐
- 19、 MySQL -- SQL statements and queries
- Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
- Introduction to golang garbage collection
- Analyse de REF nerf
- University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
- 2.3 other data types
- Reinforcement learning - learning notes 1 | basic concepts
- App compliance
- JVM JNI and PVM pybind11 mass data transmission and optimization
- How to modify the network IP addresses of mobile phones and computers?
猜你喜欢

Wargames study notes -- Leviathan

(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan

Scientific research document management Zotero

MySQL master-slave synchronization principle

What is the maximum number of concurrent TCP connections for a server? 65535?

18、 MySQL -- index

Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress

Yyds dry goods inventory TCP & UDP

Hcie security Day12: supplement the concept of packet filtering and security policy

浅议.NET遗留应用改造
随机推荐
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Golang type assertion and conversion (and strconv package)
淺析 Ref-NeRF
18、 MySQL -- index
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
QT6 QML book/qt quick 3d/ Basics
2.3 other data types
一台服务器最大并发 tcp 连接数多少?65535?
University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
[postgresql]postgresql custom function returns an instance of table type
Redis data migration (II)
浅议.NET遗留应用改造
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
强化学习-学习笔记1 | 基础概念
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
不同业务场景该如何选择缓存的读写策略?
Brief analysis of ref nerf
Example of peanut shell inner net penetration
Strange way of expressing integers (expanding Chinese remainder theorem)