当前位置:网站首页>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));
}
}
边栏推荐
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- Win10: how to modify the priority of dual network cards?
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- [C language foundation] 04 judgment and circulation
- windows下同时安装mysql5.5和mysql8.0
- La table d'exportation Navicat génère un fichier PDM
- Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
- Database middleware_ MYCAT summary
- MySQL26-性能分析工具的使用
- 【博主推荐】asp.net WebService 后台数据API JSON(附源码)
猜你喜欢

MySQL transaction log

CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)

MySQL33-多版本并发控制

Mysql27 - Optimisation des index et des requêtes

Opencv uses freetype to display Chinese

IDEA 导入导出 settings 设置文件

Database middleware_ MYCAT summary
![[Li Kou 387] the first unique character in the string](/img/2d/f2c99549cac86c08efbfbd8ba76427.jpg)
[Li Kou 387] the first unique character in the string

Mysql27 index optimization and query optimization

windows下同时安装mysql5.5和mysql8.0
随机推荐
MySQL18-MySQL8其它新特性
CSDN question and answer tag skill tree (I) -- Construction of basic framework
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
[Li Kou 387] the first unique character in the string
CSDN博文摘要(一) —— 一个简单的初版实现
Mysql 其他主机无法连接本地数据库
MySQL26-性能分析工具的使用
C language string function summary
FRP intranet penetration
++Implementation of I and i++
MySQL31-MySQL事务日志
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
Database middleware_ MYCAT summary
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
C语言标准的发展
MySQL 29 other database tuning strategies
MySQL的一些随笔记录
Idea import / export settings file
Mysql23 storage engine
windows下同时安装mysql5.5和mysql8.0