题目

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.

分析

题目输入为两个vector有序集合,要求合并两个集合(不可删除其中元素,就是说重复元素都保存在结果中),结果保存在第一个参数集合中。

本题程序实现,借用了另一个vector的空间用于保存合并后的集合,最后将此集合赋值给第一个参数集合即可。

考虑:最优算法应该是本地合并而不需要任何其他空间。

AC代码

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

GitHub测试程序源码

LeetCode(88)Merge Sorted Array的更多相关文章

  1. LeetCode(40)-Merge Sorted Array

    听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...

  2. LeetCode(108) Convert Sorted Array to Binary Search Tree

    题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  3. [LC]88题 Merge Sorted Array (合并两个有序数组 )

    ①英文题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...

  4. LeetCode(88)题解-- Merge Sorted Array

    https://leetcode.com/problems/merge-sorted-array/ 题目: Given two sorted integer arrays nums1 and nums ...

  5. LeetCode(23)Merge k Sorted Lists

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  6. LeetCode(21)Merge Two Sorted Lists

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  7. LeetCode(88):合并两个有序数组

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

  8. 【LeetCode 88_数组】Merge Sorted Array

    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { ; ; ; &&a ...

  9. LeetCode(238) Product of Array Except Self

    题目 Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...

随机推荐

  1. springboot(六)自动配置原理和@Conditional

    官方参考的配置属性:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#common-appl ...

  2. Ocelot(一)- .Net Core开源网关

    Ocelot - .Net Core开源网关 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10857688.html 源码地 ...

  3. HTML_CSS入门学习

    1 HTML 简介 下面解释什么是HTML,以及HTML标签和HTML文档的含义. 1.1 什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Te ...

  4. Bryce1010的linux课程设计

    1.设计目的 2.软件环境 3.要求 4.需求分析 5.总体设计 6.详细设计 7.调试与测试 8.总结 思路整理: 1.如果要开始编译着手的准备 SQLite数据库的安装 gtk+的安装 (.... ...

  5. QQ文件没有读取权限,60017导致QQ无法登陆的终极解决办法

    每隔一段时间,我的QQ就无法登陆,提示:QQ文件没有读取权限,60017导致QQ无法登陆的终极解决办法 点击了解详情发现里面的解决办法根本不起作用,网上 说的各种解决办法都不起作用,解决办法如下 1. ...

  6. Android 内存溢出处理方案

    转自 : http://www.cnblogs.com/hello-ruby/archive/2013/04/19/3031098.html 首先我们来看看android内存溢出的原因,有可能是: 由 ...

  7. Lucky Number Eight dp

    https://www.hackerrank.com/contests/w28/challenges/lucky-number-eight 设dp[i][v]表示前i位数中,得到余数是v的子序列的数目 ...

  8. 外文翻译 《How we decide》赛场上的四分卫 第四节

    这是第一章的最后一节. 书的导言 本章第一节 本章第二节 本章第三节 制作肥皂剧是非常不易的.整个制作组都要很紧张的工作,每天都要拍摄一些新的事件.新的大转折的剧情需要被想象出来,新的剧本需要被编写, ...

  9. 安装CentOS--设置网络_2

    (1)虽然CentOS 7已经可以联网,但是在日常的运维工作中,我们是需要手动给Linux系统设置IP地址的.输入如下命令. # vi /etc/sysconfig/network-scripts/i ...

  10. (一)Spring之初了解

    1.认识 Spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AO ...