当前位置:网站首页>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
nums1
At 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制作英雄皮肤抽奖小游戏
- tf.contrib.layers.batch_norm
- Normal equation
- leetCode-223: 矩形面积
- Leetcode-223: rectangular area
- leetCode-面试题 01.05: 一次编辑
- Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
- uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
- 【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
- [resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
猜你喜欢
5. dish management business development
Leetcode-498: diagonal traversal
Six states of threads
機械臂速成小指南(二):機械臂的應用
Leetcode-1823: find the winner of the game
Machine learning - principal component analysis (PCA)
Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default
leetCode-223: 矩形面积
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
Flink checkpoint and savepoint
随机推荐
機械臂速成小指南(二):機械臂的應用
线程的六种状态
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
整理接口性能优化技巧,干掉慢代码
抓包工具charles实践分享
Six states of threads
Sort out interface performance optimization skills and kill slow code
numpy. linspace()
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
Niuke-top101-bm29
正规方程、、、
Leetcode - 498: traversée diagonale
线程运行原理
tf. errors
4. classification management business development
Leetcode-498: diagonal traversal
【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
4.分类管理业务开发
SSM integration
Appium自动化测试基础 — 移动端测试环境搭建(一)