当前位置:网站首页>"Sprint to Dachang foundation 2"
"Sprint to Dachang foundation 2"
2022-06-09 07:24:00 【Muri】
Sprint Dachang Foundation
1. Given an ordered array , find (>=value) The leftmost position of
Ideas : Use dichotomy , Use one index Variable , Record every time (>=value) The location of , Finally back to .
public class Code05_BSNearLeft {
public static int nearestIndex(int[] arr, int value){
int L = 0;
int R = arr.length - 1;
int index = -1;
while(L <= R){
int mid = L + ((R - L) >> 1);
if(arr[mid] >= value){
index = mid;
R = mid - 1;
}else{
L = mid + 1;
}
}
return index;
}
}
2. Given an ordered array , find (<=value) Right most position of
Ideas : Use dichotomy , Use one index Variable , Record every time (<=value) The location of , Finally back to .
public class Code06_BSNearRight {
public static int nearestRight(int[] arr, int value){
int L = 0;
int R = arr.length - 1;
int index = -1;
while(L <= R){
int mid = L +((R - L) >> 1);
if(arr[mid]<= value){
index = mid;
L = mid + 1;
}else{
R = mid - 1;
}
}
return index;
}
}
3. Given an array , Only one of them appears odd number of times , All the other numbers appear an even number of times . Print the number of odd occurrences .
Ideas : The container method can be used to record the number of occurrences of each number , This is not the optimal solution .
Optimal solution : Adopt XOR method ,(0^N = N)(N ^ N = 0), And XOR operation satisfies commutative law and distributive law
public static void printOddTimesNum1(int[] arr){
int eor = 0;
for (int num : arr) {
eor ^= num;
}
System.out.println(eor);
}
4. Find the rightmost... In the specified number binary 1
Ideas : Whether positive or negative , perform N&(-N) Can be taken out N The most the right side 1, Also equivalent to N&(~N + 1).
public static int bit1Right(int N){
return N & (-N);
//return N & (~N + 1);
}
5. Given an array , Only two different numbers appear odd times , The remaining numbers appear an even number of times
Ideas : The container method can be used to record the number of occurrences of each number , This is not the optimal solution .
Optimal solution : First, XOR each number in the array , The result is the result of the two odd numbers of exclusive or (eor), Take the rightmost... In the binary of the result 1(onlyOne). Compare the numbers in the array with onlyOne Conduct & operation ,( Elements in an array )&(onlyOne) It's not equal to 0 To perform exclusive or operation on the number of .onlyOne Finally, it is one of the two numbers that appear odd times , then (eor)^(onlyOne) Get another number that occurs an odd number of times .
public static void printOddTimesNum2(int[] arr){
int eor = 0;
for (int num : arr) {
eor ^= num;
}
int rightOne = eor & (-eor);
int onlyOne = 0;
for (int num : arr) {
if((num & rightOne) != 0){
onlyOne ^= num;
}
}
System.out.println(onlyOne + " " + (eor ^ onlyOne));
}
边栏推荐
- [path of system analyst] Chapter 18 security analysis and design of double disk system (encryption and decryption)
- 工控安全行为规范
- Distributed message middleware -- Introduction to message queue
- C语言文件——字符串的方式读出与写入
- 100 Go Mistakes and How to Avoid Them 之代码结构和项目组织(四)
- 【6月第一周学习记录】UU-Computer vision(1):3D reconstruction&Camera calibration
- A collection of common test terms
- Squid代理服务器应用
- On database optimization (taking MySQL as an example)
- MongoDB基础理解
猜你喜欢

Summarize the key knowledge of multithreading.

如何解决低代码平台中的安全问题?

How to solve security problems in low code platforms?

Opengauss database operation steps

Understand the whole test process with one diagram

2022广东省安全员C证第三批(专职安全生产管理人员)考题及模拟考试

高精度人员定位系统,电厂室内定位应用解决方案

UML summary

大厂面试算法系列-动态规划求解最长公共子串问题

WPF data binding (II)
随机推荐
defineProperty
Simulated question bank and answers for the 2022 National latest fire facility operator (intermediate fire facility operator) examination
Apache配置与应用(构建web主机、日志分割及AWStats分析系统)
C# 2~36进制数转化实现
C语言fopen函数的编程实现(可直接粘贴走验证)
通达信股票开户怎么样?开户安全吗?
5. Multimedia Foundation
2022广东省安全员C证第三批(专职安全生产管理人员)考题及模拟考试
2022 national latest Fire Facility operator (Intermediate Fire Facility operator) test Simulated question base and Answers
Error (119013): Current license file does not support the EP4CE10F17C8 device解决方法
UML summary
外设驱动库开发笔记42:DAC8552 DAC驱动
Use postman to simulate a request to carry a token
Interpretation of the functions of the navigation system in the government hall
Squid proxy application
性能测试流程,指标,实现方法以及总结
2022年全國最新消防設施操作員(中級消防設施操作員)考試模擬題庫及答案
1. Talking about the system construction model -- one of the learning directions of advanced management post for testers
2022 tower crane driver (construction special type of work) examination question simulation examination question bank and simulation examination
Web performance test case design model