88. Merge Sorted Array【easy】

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 nums1 and nums2 are m and n respectively.

解法一:

 class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - ;
int j = n - ;
int len = m + n - ; if (n == )
{
return;
} while (i >= && j >=)
{
if (nums1[i] >= nums2[j])
{
nums1[len--] = nums1[i--];
}
else
{
nums1[len--] = nums2[j--];
}
} /*
while (i >= 0)
{
nums1[len--] = nums1[i--];
}
*/ while (j >= )
{
nums1[len--] = nums2[j--];
}
}
};

从后向前搞,nums2完毕之后就不用处理nums1了,因为都是往nums1中合并的

解法二:

 class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - , j = n - , tar = m + n - ;
while (j >= ) {
nums1[tar--] = i >= && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
}
}
};

This code relies on the simple observation that once all of the numbers from nums2 have been merged into nums1, the rest of the numbers in nums1 that were not moved are already in the correct place.

88. Merge Sorted Array【easy】的更多相关文章

  1. 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】

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

  2. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  3. 88 Merge Sorted Array(归并排序Easy)

    题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中 class Solution { public: void merge(vector<int>& nums ...

  4. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  5. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

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

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

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  8. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  9. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. 6.3(java学习笔记)缓冲流

    一.缓冲流 使用缓冲流后的输入输出流会先存储到缓冲区,等缓冲区满后一次性将缓冲区中的数据写入或取出. 避免程序频繁的和文件直接操作,这样操作有利于提高读写效率. 缓冲流是构建在输入输出流之上的,可以理 ...

  2. [Android Studio Problems]记录克隆项目中遇到的坑(问题)以及解决方法

    ①Migrate project to Gradle? 问题描述: This project does not use the Gradle build system. We recommend th ...

  3. Kingdee Apusic 中间件有关资料

    Kingdee Apusic 中间件有关资料: 1.官方网站:http://www.apusic.com 2.资料目录:http://www.apusic.com/dist 3.Apusic 8 资料 ...

  4. [Java基础] java多线程关于消费者和生产者

    多线程: 生产与消费 1.生产者Producer生产produce产品,并将产品放到库存inventory里:同时消费者Consumer从库存inventory里消费consume产品. 2.库存in ...

  5. iOS:自定义代码块{ }

    1.Xcode本身带有编码常用的代码块可供使用,如下图 调用方法: (1)直接拖到代码区域中: (2)使用快捷键,键入 “while”, Xcode就会出现自动完成的提示 也可以自定义自己常用的代码块 ...

  6. Modify Headers模拟不同地域进行网页测试

    今天要简单讲一下Modify Headers这个Firefox插件,记录一下我是怎么使用它的. Modify Headers: https://addons.mozilla.org/zh-CN/fir ...

  7. Golang 内存热力图

    https://cizixs.com/2017/09/11/profiling-golang-program/

  8. Elasticsearch 理解

    概述 Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的. 特性 安装方便:没有其他依赖,下载 ...

  9. jquery 中attr和prop的区别

    在jQuery API中也有专门解释: Attributes VS. Properties 在一些特殊的情况下,attributes和properties的区别非常大.在jQuery1.6之前,.at ...

  10. Android重写ViewPager改动滑动灵敏度

        使用ViewPager作为一个页面进行切换.里面能够存放非常多View,但有时在操作View时不小心滑动一下就有可能跳到下一页,这并非我们想要的,这里就须要重写ViewPager改动它的滑动条 ...