Leetcode88_Merge Sorted Array_Easy
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
- The number of elements initialized in nums1 and nums2 are m and n respectively.
- You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
ExamplInpunums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
思路:
定义三个指针i,j,k,分别指向nums1,nums2,混合数组的末尾。
当nums2有剩余,继续循环,将剩余元素排入混合数组。
nums1有剩余,不用管,因为它本身包含在混合数组里。
注意:
第一个while
while (i >= 0 && j >= 0)
确保nums1/nums2其中一个数组都插进混合数组。不可以
while ( i != 0 && j !=0)
第二个while条件是 j >= 0,也是为了把所有元素插入。
代码:
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1; j = n - 1; k = m + n - 1;
while (i >= 0 && j >= 0){
if (nums1[i] >= nums2[j])
nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}
while (j >= 0)
nums1[k--] = nums2[j--];
}
}
Leetcode88_Merge Sorted Array_Easy的更多相关文章
- [LeetCode] 88. Merge Sorted Array_Easy tag: Two Pointers
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- python os.path.basename()方法
返回path最后的文件名.如果path以/或\结尾,那么就会返回空值.即os.path.split(path)的第二个元素. >>> import os >>> p ...
- 在HUE中将文本格式的数据导入hive数仓中
今天有一个需求需要将一份文档形式的hft与fdd的城市关系关系的数据导入到hive数仓中,之前没有在hue中进行这项操作(上家都是通过xshell登录堡垒机直接连服务器进行操作的),特此记录一下. - ...
- 【Alpha版本】冲刺阶段——Day4
[Alpha版本]冲刺阶段--Day4 阅读目录 今日进展 问题困难 明日任务 今日贡献量 TODOlist [今日进展] 完成注册类代码 public class Register { privat ...
- SEO三种职位类型:编辑型SEO、技术型SEO、营销型SEO详解
SEO三种职位类型:编辑型SEO.技术型SEO.营销型SEO详解 网站SEO优化作为营销端的服务之一,这些年也呈现出日新月异的格局.一改过去游兵散将式的小作坊生产模式,不断有力量强大的公司团体加入到这 ...
- usb通信小结
2010-07-25 16:52:00 目前了解了usb通信层面的一些基础知识如下.如果有空还要再了解hid报告描述符及协议的数据包波形. 一,USB的一些基本概念 1. 管道(Pipe) 是主机和设 ...
- 算法训练 P0505
一个整数n的阶乘可以写成n!,它表示从1到n这n个整数的乘积.阶乘的增长速度非常快,例如,13!就已经比较大了,已经无法存放在一个整型变量中:而35!就更大了,它已经无法存放在一个浮点型变量中.因此, ...
- 每天学点Linux命令之grep 和 wc命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...
- Oracle12.2中新增的分区功能
Oracle 12.2已经发布一段时间,公网上也可以下载试用.针对12.2,partitioning(分区)也有了不少增强. 自动列表分区 多字段列表分区 只读分区 分区维护时允许过滤 在线转换非分区 ...
- JDK 的配置和反编译工具的使用---------------Java知识点
初始Java 1995年5月,sun公司开发了一门新的编程语言------Java 詹姆斯.高斯林(Java之父),Java语言小巧安全具有可移植可跨平台性的优点. 开发java程序的步骤:编写 ,编 ...
- pytorch种, 一维Conv1d, 二维Conv2d
pytorch之nn.Conv1d详解 之前学习pytorch用于文本分类的时候,用到了一维卷积,花了点时间了解其中的原理,看网上也没有详细解释的博客,所以就记录一下. Conv1dclass tor ...