题目

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. vs2013问题解决办法—>fatal error LNK1168 如何避免

    问题:在使用visial studio 2013运行成功程序,但当修改其中的一段后,即语法,执行后报错  出现error:fatal error LNK1168:无法打开……\xxx.exe进行写入. ...

  2. [ZJOI2011]道馆之战

    Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次.当一个冰地上的所有冰块都被经过之后,到下一个 ...

  3. 实现字符串的查找和替换 分类: c/c++ 2014-10-09 22:33 469人阅读 评论(0) 收藏

    在字符串中查找目标字符串并将其替换为指定字符串,返回替换的次数.接口为 int find_str_replace(char *&str,const char *find_str,const c ...

  4. @GetMapping和@PostMapping 和@RequestMapping区别

    @GetMapping 用于将HTTP GET请求映射到特定处理程序方法的注释. 具体来说,@GetMapping是一个作为快捷方式的组合注释@RequestMapping(method = Requ ...

  5. SpringMVC与请求控制器

    MVC设计模式 视图(View)      -对应组件:JSP或者HTML文件 控制器(controller) -对应组件:Servlet 模型(Model)   -对应组件:JavaBean MVC ...

  6. iOS Programming Dynamic Type 1

    iOS Programming Dynamic Type 1  Dynamic Type is a technology introduced in iOS 7 that helps realize ...

  7. 修改vim注释字体颜色

    vim /etc/vimrc#最后一行新增代码块hi comment ctermfg=6 注:注释用 " 标注 PS:默认的注释颜色是4  然后有0,1,2,3,4,5,6,7来选择.可以除 ...

  8. postgresql update from

    1,update   from   关联表的更新 update table a set name=b.name from table B b  where a.id=b.id; update test ...

  9. Delphi win10 asssertion failure

    Delphi2007 原来安装在Win7 下 运行正常, 自从升级到Win10 ,新建工程运行然后关闭报错, 报错信息如下: ---------------------------bds.exe - ...

  10. win+r 快速命令

    control keymgr.dll   打开凭据管理器 secpol.msc   本地安全策略 mstsc   远程 msconfig   启动选项 %temp%   临时文件夹 \\192.168 ...