当前位置:网站首页>88. merge ordered arrays
88. merge ordered arrays
2022-06-24 10:33:00 【Sit at a sunny window and drink tea alone】

Be careful : In thinking , First realize the main idea , Think about the details ( Boundary situation )

Main idea :
- Compare the ends of two arrays , Whichever is larger will be filled in
nums1At the end of
Details :
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1, j = n - 1;
for(int k = m + n - 1 ; k >= 0 ; k--) {
if (j < 0 || (i >= 0 && nums1[i] > nums2[j])) {
nums1[k] = nums1[i];
i--;
}else{
nums1[k] = nums2[j];
j--;
}
}
}
}
You can see the code above , There are... Variables 3 individual , i , j , k among k Is always greater than 0 Of therefore k The value is that the array will not cross the bounds during the whole process .
But for i and j There is no limit So it is necessary to judge the boundary conditions ,i and j Must be greater than or equal to 0 Can only be
j < 0 Express nums2 The elements of the array have been merged
边栏推荐
- HBuilder制作英雄皮肤抽奖小游戏
- pycharm快捷键大全
- Difference between package type and basic type
- 用扫描的方法分发书稿校样
- Leetcode-2221: triangular sum of arrays
- 机械臂速成小指南(零):指南主要内容及分析方法
- Illustration miscellaneous [for archiving to prevent loss]
- How to customize sharing links in wechat for H5 web pages
- 抓包工具charles實踐分享
- SF Technology Smart logistics Campus Technology Challenge (June 19, 2022) [AK]
猜你喜欢

【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)

JMeter接口测试工具基础— 取样器sampler(二)

正规方程、、、

SQL Server AVG function rounding

Appium automation test foundation - mobile end test environment construction (I)

【JS逆向分享】某个网站社区信息

线程运行原理
![Illustration miscellaneous [for archiving to prevent loss]](/img/cf/bf48bfda020a3c8690339a388efc5d.png)
Illustration miscellaneous [for archiving to prevent loss]

Quick completion guide for mechanical arm (I): development overview of mechanical arm

Hill sorting graphic explanation + code implementation
随机推荐
[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)
线程的六种状态
机械臂速成小指南(二):机械臂的应用
分布式事务原理以及解决分布式事务方案
3. addition, deletion, modification and query of employees
leetCode-面试题 16.06: 最小差
学习整理在php中使用KindEditor富文本编辑器
Flink checkPoint和SavePoint
机械臂速成小指南(一):机械臂发展概况
leetCode-223: 矩形面积
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
Record the range of data that MySQL update will lock
283.移动零
【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
leetCode-929: 独特的电子邮件地址
Leetcode-498: diagonal traversal
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
SSM整合
【JS逆向分享】某个网站社区信息