当前位置:网站首页>Remember a company interview question: merge ordered arrays
Remember a company interview question: merge ordered arrays
2022-07-06 11:00:00 【Sanli akaman】
source
2021/09/24: Received an interview from a company , Tear one by hand to the problem of merging ordered arrays , At that time, it was almost done , The time given by the interviewer is relatively short , There is a problem with the critical value processing , It's a pity that I didn't write it out completely , But at least I have the right idea , Refer to the information on the Internet , Now we have sorted out a better solution , I just wrote some test cases , If there is an error , Please correct me !
No more specific topics , Just look at the title
Code up
package com.vleus.algorithm.strings;
import java.util.Arrays;
/** * @author vleus * @date 2021 year 09 month 24 Japan 19:50 */
public class Solution {
// Two ordered arrays , Merge into an ordered array , The required time complexity is O(n)
public static int[] getNewArr(int[] arr1, int[] arr2) {
if (arr1.length == 0) {
return arr2;
}
if (arr2.length == 0) {
return arr1;
}
int i = 0;
int j = 0;
int[] newArr = new int[arr1.length + arr2.length];
for (int k = 0; k < newArr.length; k++) {
if (arr1[i] < arr2[j] && i <= arr1.length - 1) {
newArr[k] = arr1[i];
i++;
continue;
}
if (arr1[i] >= arr2[j] && j <= arr2.length - 1) {
newArr[k] = arr2[j];
j++;
continue;
}
}
return newArr;
}
public static int[] getNewArr2(int[] arr1, int[] arr2) {
int i = arr1.length + arr2.length - 1;
int i1 = arr1.length - 1;
int i2 = arr2.length - 1;
int[] newArr = new int[arr1.length+arr2.length];
while (i1 >= 0 && i2 >= 0) {
if (arr1[i1] >= arr2[i2]) {
newArr[i] = arr1[i1];
i1--;
i--;
}else{
newArr[i] = arr2[i2];
i2--;
i--;
}
}
if(i1 >= 0){
System.arraycopy(arr1,0,newArr,i1,i1+1);
}
if (i2 >= 0) {
System.arraycopy(arr2,0,newArr,i2,i2+1);
}
return newArr;
}
public static void main(String[] args) {
int[] arr1 = new int[]{
1, 3, 5, 7, 7, 8,12};
int[] arr2 = new int[]{
2, 4, 6, 8, 8};
int[] newArr2 = getNewArr2(arr1, arr2);
System.out.println(Arrays.toString(newArr2));
}
}
边栏推荐
- Have you mastered the correct posture of golden three silver four job hopping?
- Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
- Solve the problem that XML, YML and properties file configurations cannot be scanned
- Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
- Mysql25 index creation and design principles
- Mysql27 index optimization and query optimization
- Pytorch RNN actual combat case_ MNIST handwriting font recognition
- 虚拟机Ping通主机,主机Ping不通虚拟机
- C language string function summary
- 【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)
猜你喜欢

CSDN question and answer module Title Recommendation task (II) -- effect optimization

Mysql26 use of performance analysis tools

Emotional classification of 1.6 million comments on LSTM based on pytoch

Invalid global search in idea/pychar, etc. (win10)

Navicat 导出表生成PDM文件

Install mysql5.5 and mysql8.0 under windows at the same time
![[Thesis Writing] how to write function description of jsp online examination system](/img/f8/13144e0febf4a576bbcc3290192079.jpg)
[Thesis Writing] how to write function description of jsp online examination system

Mysql23 storage engine
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

Bytetrack: multi object tracking by associating every detection box paper reading notes ()
随机推荐
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
CSDN问答标签技能树(五) —— 云原生技能树
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
MySQL completely uninstalled (windows, MAC, Linux)
Mysql24 index data structure
Kubernetes - problems and Solutions
MySQL master-slave replication, read-write separation
Mysql22 logical architecture
windows下同时安装mysql5.5和mysql8.0
FRP intranet penetration
Some problems in the development of unity3d upgraded 2020 VR
Adaptive Bezier curve network for real-time end-to-end text recognition
Ansible实战系列一 _ 入门
Global and Chinese market for intravenous catheter sets and accessories 2022-2028: Research Report on technology, participants, trends, market size and share
[recommended by bloggers] C WinForm regularly sends email (with source code)
MySQL的一些随笔记录
Solution: log4j:warn please initialize the log4j system properly
Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
Postman Interface Association