当前位置:网站首页>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};
}
}
边栏推荐
- Golang anonymous function use
- nifi从入门到实战(保姆级教程)——flow
- Chinese translation of Tagore's floating birds (1~10)
- QT串口ui设计和解决显示中文乱码
- 【剑指 Offer 】64. 求1+2+…+n
- Record windows10 installation tensorflow-gpu2.4.0
- Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
- Cocos Creator 2. X automatic packaging (build + compile)
- PHP secondary domain name session sharing scheme
猜你喜欢
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
The word backspace key cannot delete the selected text, so you can only press Delete
Visual SLAM algorithms: a survey from 2010 to 2016
什么是质押池,如何进行质押呢?
Mb10m-asemi rectifier bridge mb10m
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
MySQL single table field duplicate data takes the latest SQL statement
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Getting started with Message Oriented Middleware
随机推荐
Mysql 将逗号隔开的属性字段数据由列转行
Develop team OKR in the way of "crowdfunding"
Interviewer: how does the JVM allocate and recycle off heap memory
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
消息队列消息丢失和消息重复发送的处理策略
Page dynamics [2]keyframes
Unity项目优化案例一
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Data driving of appium framework for mobile terminal automated testing
利用MySQL中的乐观锁和悲观锁实现分布式锁
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
Getting started with Message Oriented Middleware
nifi从入门到实战(保姆级教程)——flow
PHP二级域名session共享方案
Visual SLAM algorithms: a survey from 2010 to 2016
(补)双指针专题
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
word 退格键删除不了选中文本,只能按delete