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 ...
随机推荐
- ID3决策树
决策树 优点:计算复杂度不高,输出结果易于理解,对中间值的缺少不敏感,可以处理不相关特征数据 缺点:过拟合 决策树的构造 熵:混乱程度,信息的期望值 其中p(xi)是选择分类的概率 熵就是计算所有类别 ...
- 新浪微博 [异常问题] 414 Request-URL Too Large
新浪微博 [异常问题] 414 Request-URL Too Large 浏览器上打开新浪微博,或则日志是返回结果提示:414 Request-URL Too Large原因:因同IP访问微博页面过 ...
- js获取浏览器类型和版本信息
bro () { let broName = 'Runing' let strStart = 0 let strStop = 0 let temp = '' let userAgent = windo ...
- 瀑布流之ajax
wf_js.html(展示页) <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...
- selenium webdriver 实现Canvas画布自动化测试
https://blog.csdn.net/xiaoguanyusb/article/details/80324210 由借鉴意义, 转过来 canvas 是一个画布,定位元素时只能定位到画布上,如下 ...
- layui使用iconfont
layui的图标取自于阿里巴巴的矢量图标库 Iconfont,同样的,这篇教程也是基于Iconfont进行扩展. 第一步,通过浏览器打开 http://iconfont.cn/ ,访问阿里巴巴矢量图标 ...
- bzoj1663: [Usaco2006 Open]赶集
Description Every year, Farmer John loves to attend the county fair. The fair has N booths (1 <= ...
- android之csv导出
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Cannot add foreign key constraint @ManyToMany @OneToMany
最近在使用shiro做权限管理模块时,使用的时user(用户)-role(角色)-resource(资源)模式,其中user-role 是多对多,role-resource 也是多对多.但是在使用sp ...
- Python3 tkinter基础 Canvas create_text 在画布上添加文字
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...