当前位置:网站首页>2022.02.14_ Daily question leetcode five hundred and forty
2022.02.14_ Daily question leetcode five hundred and forty
2022-07-03 16:36:00 【Promise い】
Title Description
- A single element in an ordered array
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 : 10
Tips :
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^5
Answer key 1
Ideas
Use dichotomy , Ensure that the array is in left To right The length of is odd , Through the middle mid Divide the boundary , Judge the length of the left and right sub arrays respectively , Judge the corresponding according to the length mid Nearby values , Find out the result should be in mid Left or right .
Code
class Solution {
public int singleNonDuplicate(int[] nums) {
if (nums.length == 1){
return nums[0];
}
int left = 0;
int right = nums.length - 1;
while (left < right){
int mid = left + ((right - left) >> 1);
if ((mid - left + 1) % 2 == 0){
if (nums[mid] != nums[mid - 1]){
right = mid - 1;
}else {
left = mid + 1;
}
}else {
if (nums[mid] != nums[mid - 1]){
left = mid;
}else {
right = mid;
}
}
}
return nums[left];
}
}
Running results

Answer key 2
Ideas
Because the array is ordered , Therefore, it can be judged by every two numbers , According to whether the XOR result is 0 To determine the result
principle :
N ^ N == 0
N ^ 0 == N
(A ^ B) ^ C == A ^ (B ^ C)
A ^ B == B ^ A
Code
class Solution {
public int singleNonDuplicate(int[] nums) {
int res = 0;
for (int i : nums) {
res = i ^ res;
}
return res;
}
}
Running results

expand
package com.nuo.Demo;
import java.util.Arrays;
import java.util.Stack;
/** * @author nuo * @version 1.0 * @description: TODO XOR search * @date 2022/1/5 16:49 */
public class s_02{
// An array of integers , Only one number appears an odd number of times , Other numbers appear an even number of times , Find this number
public static int select_1(int[] arr) {
int eor = 0;
for (int i : arr) {
eor = eor ^ i;
}
return eor;
}
// An array of integers , There are only two numbers (a , b) An odd number of times , Other numbers appear an even number of times , Find this number
public static int[] select_2(int[] arr) {
int eor = 0;
for (int i : arr) {
eor = eor ^ i;
} // => eor = a ^ b != 0 => eor One must be '1'
// Original code & Complement code
int rightOne = eor & (~eor + 1); // => Extract the rightmost 1 , The number of these two is different
// eg. eor = 1001 => ~eor = 0110 => ~eor + 1 = 0111 => eor & (~eor + 1) = 0001
int eor_ = 0;
for (int j : arr) {
if ((rightOne & j) == 0) {
eor_ = eor_ ^ j;
}
}
return new int[]{
eor_ , eor_ ^ eor};
}
}
边栏推荐
- [Jianzhi offer] 57 - ii Continuous positive sequence with sum s
- Explore Cassandra's decentralized distributed architecture
- Visual SLAM algorithms: a survey from 2010 to 2016
- [sword finger offer] 58 - I. flip the word order
- Visual SLAM algorithms: a survey from 2010 to 2016
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- 探索Cassandra的去中心化分布式架构
- Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
- The mixlab editing team is recruiting teammates~~
- NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
猜你喜欢

(Supplement) double pointer topic

Uploads labs range (with source code analysis) (under update)

8 cool visual charts to quickly write the visual analysis report that the boss likes to see

斑馬識別成狗,AI犯錯的原因被斯坦福找到了

关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM

Unreal_ Datatable implements ID self increment and sets rowname

Multithread 02 thread join

(补)双指针专题

TCP congestion control details | 3 design space
![[statement] about searching sogk1997 and finding many web crawler results](/img/1a/8ed3ca0030ea227adcd95e8b306aca.png)
[statement] about searching sogk1997 and finding many web crawler results
随机推荐
Mongodb installation and basic operation
相同切入点的抽取
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
Svn usage specification
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
One article takes you to understand machine learning
【剑指 Offer】58 - II. 左旋转字符串
Golang anonymous function use
利用MySQL中的乐观锁和悲观锁实现分布式锁
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Détails du contrôle de la congestion TCP | 3. Espace de conception
Unity project optimization case 1
A survey of state of the art on visual slam
TCP擁塞控制詳解 | 3. 設計空間
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
程序猿如何快速成长
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
【LeetCode】94. Middle order traversal of binary tree
How programming apes grow rapidly
Processing strategy of message queue message loss and repeated message sending