Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1and nums2 are m and n respectively.

思路:这题明显归并排序的影子,仅仅是须要变通一下,从后往前排序就可以。将大的值放在nums1后面。

代码例如以下:

public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) { /**
* 归并排序的思想,从后往前排
* 大的放在最后。依次放到最前
*/
int len = m + n;//排序后总长度
while(m > 0 && n > 0){
if(nums1[m-1] > nums2[n-1]){
nums1[--len] = nums1[--m];
}else{
nums1[--len] = nums2[--n];
}
}
//余下的数字
while(m > 0){
nums1[--len] = nums1[--m];
}
while(n > 0){
nums1[--len] = nums2[--n];
}
}
}

leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法的更多相关文章

  1. [LeetCode] 88. Merge Sorted Array 合并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  2. [leetcode]88. Merge Sorted Array归并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  3. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  4. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  5. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  6. LeetCode 088 Merge Sorted Array 合并两个有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...

  7. LeetCode 88 Merge Sorted Array

    Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  8. LeetCode 之 Merge Sorted Array(排序)

    [问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  9. Leetcode 88 Merge Sorted Array STL

    合并有序数组 时间复杂度O(m+n) 该算法来自各种算法与数据结构书,写得已经烂得不能再烂了,这个应该是最短的代码了吧,不知如何归类 class Solution { public: void mer ...

随机推荐

  1. PE笔记之NT头PE文件头

    typedef struct _IMAGE_FILE_HEADER {       WORD    Machine;                         //014C-IMAGE_FILE ...

  2. 模拟浏览器的GET和POST动作

    Jakarta的httpclient3.1是最新版本,项目中需要用程序模拟浏览器的GET和POST动作.在使用过程中遇到不少问题.1. 带附件的POST提交    最开始都是使用MultipartPo ...

  3. hdu 4497(排列组合+LCM和GCD)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  4. LeetCode OJ-- Container With Most Water

    https://oj.leetcode.com/problems/container-with-most-water/ 不同高度的柱子排一列,两个柱子可以组成一个容器,求最大容积. 最直观的方法就是暴 ...

  5. AC日记——营业额统计 codevs 1296 (splay版)

    营业额统计 思路: 每次,插入一个点: 然后找前驱后继: 来,上代码: #include <cmath> #include <cstdio> #include <iost ...

  6. C++ primer分章节快速回顾

    第三章: 1,sozeof(int): int n_int=INT_MAX; sizeof n_int;(对变量括号可选) 2,#include<climits>包含一些类型的最大值3,c ...

  7. Go语言调度器之主动调度(20)

    本文是<Go语言调度器源代码情景分析>系列的第20篇,也是第五章<主动调度>的第1小节. Goroutine的主动调度是指当前正在运行的goroutine通过直接调用runti ...

  8. codevs——2841 愤怒的LJF(背包)

    样例有误!  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description LJF发现ZPC的积分比他高,他很愤怒. 他挤出T ...

  9. 3)nginx的启动与停止、重启,linux配置对外端口

    [启动] 启动代码格式:nginx安装目录地址 -c nginx配置文件地址例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /u ...

  10. 解决php中redis client进行subscribe操作出现timeout的问题

    出现该问题的原因是poll设置接收超时所致,这个超时默认设置60s 设置Redis::OPT_READ_TIMEOUT配置项: 解决方法如下: <?php $redis = new Redis( ...