当前位置:网站首页>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
边栏推荐
- Quick completion guide for mechanical arm (I): development overview of mechanical arm
- Distributed transaction principle and solution
- 88.合并有序数组
- 栈题目:括号的分数
- leetCode-929: 独特的电子邮件地址
- [EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
- Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
- Niuke-top101-bm28
- 包装类型与基本类型的区别
- SQL Server AVG function rounding
猜你喜欢

Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.

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

charles抓包工具使用教程

2022 International Symposium on intelligent robots and systems (isoirs 2022)
![[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)](/img/48/11bd949fa3695452270c3625d63e6d.jpg)
[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)

88.合并有序数组

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

Sort out interface performance optimization skills and kill slow code

Leetcode-498: diagonal traversal

Leetcode - 498: traversée diagonale
随机推荐
Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
牛客-TOP101-BM28
图解杂项【防止丢失进行存档用的】
cuda runtime error (801) : Raw out
JMeter接口测试工具基础 — Badboy工具
[EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
牛客-TOP101-BM29
静态链接库和动态链接库的区别
Web site development, lazy image loading
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
3. addition, deletion, modification and query of employees
23. Opencv——图像拼接项目
Leetcode-1823: find the winner of the game
Leetcode-1051: height checker
Niuke-top101-bm29
leetCode-1823: 找出游戏的获胜者
H5网页如何在微信中自定义分享链接
Appium自动化测试基础 — 移动端测试环境搭建(一)
顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)