当前位置:网站首页>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
边栏推荐
- Uniapp implementation forbids video drag fast forward
- 抓包工具charles实践分享
- 顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
- Using pandas to read SQL server data table
- 跨域概述,简单积累
- Leetcode-223: rectangular area
- SQL Server AVG function rounding
- 消息队列的作用
- Leetcode-498: diagonal traversal
- Leetcode interview question 01.05: primary editing
猜你喜欢

机械臂速成小指南(二):机械臂的应用

线程的 sleep() 方法与 wait() 方法的区别

Record the range of data that MySQL update will lock

整理接口性能优化技巧,干掉慢代码

charles抓包工具使用教程

1.项目环境搭建

Resolved: methods with the same name as their class will not be constructors in

Baidu online disk download has been in the process of requesting solutions

【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)

leetCode-2221: 数组的三角和
随机推荐
International Symposium on energy and environmental engineering in 2022 (coeee 2022)
numpy. linspace()
Illustration miscellaneous [for archiving to prevent loss]
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
Flink checkpoint and savepoint
Appium automation test foundation - mobile end test environment construction (I)
numpy. logical_ or
3.员工的增删改查
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
2. login and exit function development
Practice sharing of packet capturing tool Charles
numpy.logical_or
消息队列的作用
【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
Uniapp implementation forbids video drag fast forward
numpy.linspace()
希尔排序图文详解+代码实现
126. 单词接龙 II BFS
[IEEE publication] 2022 International Conference on service robots (iwosr 2022)
抓包工具charles实践分享