当前位置:网站首页>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};
}
}
边栏推荐
- 爱可可AI前沿推介(7.3)
- Expression of request header in different countries and languages
- Record windows10 installation tensorflow-gpu2.4.0
- Multithread 02 thread join
- 0214-27100 a day with little fluctuation
- NSQ源码安装运行过程
- [combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
- 面试官:JVM如何分配和回收堆外内存
- The mixlab editing team is recruiting teammates~~
- [Jianzhi offer] 58 - ii Rotate string left
猜你喜欢
什么是质押池,如何进行质押呢?
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
The word backspace key cannot delete the selected text, so you can only press Delete
一台服务器最大并发 tcp 连接数多少?65535?
2022爱分析· 国央企数字化厂商全景报告
Colab works with Google cloud disk
Explore Cassandra's decentralized distributed architecture
记一次jar包冲突解决过程
Cocos Creator 2. X automatic packaging (build + compile)
What is the pledge pool and how to pledge?
随机推荐
如何在本机搭建SVN服务器
PHP CI (CodeIgniter) log level setting
Learn from me about the enterprise flutter project: simplified framework demo reference
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
【剑指 Offer 】64. 求1+2+…+n
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
Client does not support authentication protocol requested by server; consider upgrading MySQL client
[Jianzhi offer] 58 - ii Rotate string left
Unity project optimization case 1
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Chinese translation of Tagore's floating birds (1~10)
【剑指 Offer】58 - I. 翻转单词顺序
Add color to the interface automation test framework and realize the enterprise wechat test report
One article takes you to understand machine learning
TCP擁塞控制詳解 | 3. 設計空間
Qt插件之自定义插件构建和使用
PHP二级域名session共享方案
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
PHP secondary domain name session sharing scheme
切入点表达式