当前位置:网站首页>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};
}
}
边栏推荐
- Multithread 02 thread join
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- 手机注册股票开户安全吗 开户需要钱吗
- Golang 装饰器模式以及在NSQ中的使用
- Threejs Part 2: vertex concept, geometry structure
- TCP拥塞控制详解 | 3. 设计空间
- Learn from me about the enterprise flutter project: simplified framework demo reference
- 一台服务器最大并发 tcp 连接数多少?65535?
- [Jianzhi offer] 64 Find 1+2+... +n
- 消息队列消息丢失和消息重复发送的处理策略
猜你喜欢
arduino-esp32:LVGL项目(一)整体框架
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
Mixlab编辑团队招募队友啦~~
word 退格键删除不了选中文本,只能按delete
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
[web security] - [SQL injection] - error detection injection
Getting started with Message Oriented Middleware
Mysql 单表字段重复数据取最新一条sql语句
Explore Cassandra's decentralized distributed architecture
Add color to the interface automation test framework and realize the enterprise wechat test report
随机推荐
(Supplement) double pointer topic
14 topics for performance interviews between superiors and subordinates (4)
切入点表达式
消息队列消息丢失和消息重复发送的处理策略
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
A survey of state of the art on visual slam
Is it safe to open an account with tongdaxin?
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
1287. Elements that appear more than 25% in an ordered array
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Stm32f103c8t6 firmware library lighting
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
EditText request focus - EditText request focus
Cocos Creator 2. X automatic packaging (build + compile)
Mysql 单表字段重复数据取最新一条sql语句
NSQ源码安装运行过程
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
PHP secondary domain name session sharing scheme
Golang anonymous function use