当前位置:网站首页>记一次某公司面试题:合并有序数组
记一次某公司面试题:合并有序数组
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));
}
}
边栏推荐
- 【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)
- MySQL20-MySQL的数据目录
- Mysql22 logical architecture
- API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
- MySQL23-存儲引擎
- MySQL完全卸载(Windows、Mac、Linux)
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- FRP intranet penetration
- Mysql30 transaction Basics
- Mysql33 multi version concurrency control
猜你喜欢
Win10: how to modify the priority of dual network cards?
MySQL主从复制、读写分离
csdn-Markdown编辑器
CSDN question and answer module Title Recommendation task (II) -- effect optimization
CSDN问答模块标题推荐任务(二) —— 效果优化
Valentine's Day is coming, are you still worried about eating dog food? Teach you to make a confession wall hand in hand. Express your love to the person you want
[recommended by bloggers] background management system of SSM framework (with source code)
Mysql32 lock
Emotional classification of 1.6 million comments on LSTM based on pytoch
Postman uses scripts to modify the values of environment variables
随机推荐
CSDN博文摘要(一) —— 一个简单的初版实现
Copy constructor template and copy assignment operator template
MySQL completely uninstalled (windows, MAC, Linux)
【博主推荐】C#生成好看的二维码(附源码)
CSDN question and answer module Title Recommendation task (II) -- effect optimization
Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
API learning of OpenGL (2002) smooth flat of glsl
C language string function summary
Why is MySQL still slow to query when indexing is used?
Ubuntu 20.04 安装 MySQL
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
[paper reading notes] - cryptographic analysis of short RSA secret exponents
虚拟机Ping通主机,主机Ping不通虚拟机
Installation and use of MySQL under MySQL 19 Linux
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Mysql35 master slave replication
Mysql33 multi version concurrency control
Copie maître - esclave MySQL, séparation lecture - écriture
MySQL22-逻辑架构
C语言标准的发展