当前位置:网站首页>记一次某公司面试题:合并有序数组
记一次某公司面试题:合并有序数组
2022-07-06 09:13:00 【三笠·阿卡曼】
来源
2021/09/24:接到某公司面试,手撕一到合并有序数组的题,当时做的差不多了,面试官时间给的相对短了一些,临界值处理有问题, 没完全写出来有些遗憾,不过至少自己思路没错,参考网上给出的资料,现整理出一个比较好的解决方式,自己也只是写了一点测试用例,若有错误,烦请指正!
具体题目不讲了,就看题目标题吧
上代码
package com.vleus.algorithm.strings;
import java.util.Arrays;
/** * @author vleus * @date 2021年09月24日 19:50 */
public class Solution {
//两个有序数组,合并成一个有序数组,要求时间复杂度为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));
}
}
边栏推荐
- MySQL transaction log
- MySQL32-锁
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
- Mysql 其他主机无法连接本地数据库
- @controller,@service,@repository,@component区别
- npm一个错误 npm ERR code ENOENT npm ERR syscall open
- MySQL completely uninstalled (windows, MAC, Linux)
- Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
- La table d'exportation Navicat génère un fichier PDM
猜你喜欢

Other new features of mysql18-mysql8

MySQL36-数据库备份与恢复
![[C language foundation] 04 judgment and circulation](/img/59/4100971f15a1a9bf3527cbe181d868.jpg)
[C language foundation] 04 judgment and circulation

解决:log4j:WARN Please initialize the log4j system properly.

Mysql36 database backup and recovery

Database middleware_ MYCAT summary

35 is not a stumbling block in the career of programmers

MySQL31-MySQL事务日志

Mysql32 lock

How to find the number of daffodils with simple and rough methods in C language
随机推荐
MySQL27-索引優化與查詢優化
MySQL23-存储引擎
Install mysql5.5 and mysql8.0 under windows at the same time
CSDN问答标签技能树(五) —— 云原生技能树
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Redis的基础使用
Ansible practical Series II_ Getting started with Playbook
CSDN question and answer module Title Recommendation task (II) -- effect optimization
SSM整合笔记通俗易懂版
CSDN问答标签技能树(一) —— 基本框架的构建
[reading notes] rewards efficient and privacy preserving federated deep learning
Texttext data enhancement method data argument
CSDN blog summary (I) -- a simple first edition implementation
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Why is MySQL still slow to query when indexing is used?
虚拟机Ping通主机,主机Ping不通虚拟机
Baidu Encyclopedia data crawling and content classification and recognition
MySQL 29 other database tuning strategies
A trip to Macao - > see the world from a non line city to Macao
Discriminant model: a discriminant model creation framework log linear model