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 读csv文件对列名进行合法性验证
如果正在读取CSV 数据并将它们转换为命名元组,需要注意对列名进行合法性认证.例如,一个CSV 格式文件有一个包含非法标识符的列头行,这样最终会导致在创建一个命名元组时产生一个ValueError 异 ...
- vue使用tradingview开发K线图相关问题
vue使用tradingview开发K线图相关问题 1.TradingView中文开发文档https://b.aitrade.ga/books/tradingview/CHANGE-LOG.html2 ...
- Powerpoint 演示时定时提醒工具
经常碰到这样的场景,规定的演讲报告时间所剩无几,甚至是已经超时,但演讲者并不知情,做为主持人只能从旁边轻轻的善意的提醒,但有时演讲者会没注意到主持人的提醒... 这里要介绍的就是这样一款用于提醒演讲者 ...
- Spring Boot 中使用 @ConfigurationProperties 注解
@ConfigurationProperties 主要作用:绑定 application.properties 中的属性 例如: @Configuration public class DataSou ...
- 事务 c#
事务->:事务是恢复和并发控制的基本单位 ->事务具有四个特性:原子性.隔离性.一致性.持久性.这四个特性通常称为ACID Begin transaction/tran --开始事务 ...
- 计算概论(A)/基础编程练习2(8题)/7:整数的个数
#include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...
- Vue小案例 之 商品管理------创建页面与部分数据
logo的路径: 页面的初始布局: 初始的HTML: <div id="container"> <!--logo title--> <div clas ...
- 05: 使用axios/vue-resource发送HTTP请求
1.1 axios 简介与安装 1.axios简介 1. vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 2. axios是一个基于Promise的HTTP请 ...
- jq svg 修改image的xmlns:xlink及图片的显隐
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- day 27 异常处理
一.异常 1.什么是异常? 异常指的是与正常情况不同在程序中 程序的正常执行过程 按照代码顺序 一行一行的执行 直到所有的代码都执行完如果在执行过程中出现了错误导致代码无法执行完毕 这就称之为异常异常 ...