LeetCode 题解 | 面试题 10.01. 合并排序的数组
给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳 B。 编写一个方法,将 B 合并入 A 并排序。
初始化 A 和 B 的元素数量分别为 m 和 n。
示例:
输入:
A = [1,2,3,0,0,0], m = 3
B = [2,5,6], n = 3输出: [1,2,2,3,5,6]
方法一:
class Solution {
public:
void merge(vector<int>& A, int m, vector<int>& B, int n) {
int C=m;
for(int i=0;i<n;++i){
A[C++]=B[i];
}
sort(A.begin(),A.begin()+n+m);
for(int i=0;i<m+n;++i){
printf("%d ",A[i]);
}
printf("\n");
}
};
执行用时 :4 ms, 在所有 C++ 提交中击败了78.97%的用户
内存消耗 :11.6 MB, 在所有 C++ 提交中击败了100.00%的用户
方法二:
class Solution {
public:
void merge(vector<int>& A, int m, vector<int>& B, int n) {
for(int i=0;i<B.size();i++)
A.pop_back();
A.insert(A.begin(),B.begin(),B.end());
sort(A.begin(),A.end());
}
};
执行用时 :0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗 :11.5 MB, 在所有 C++ 提交中击败了100.00%的用户
LeetCode 题解 | 面试题 10.01. 合并排序的数组的更多相关文章
- Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组
Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组 Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组 思路 这道题,两个数组原本就有序.于是我们采用双指针 ...
- [LeetCode] 面试题 10.01.合并排序的数组
题目: 这道题有多种实现的思路,这里使用双指针结合数组有序的特点进行解决 思路: m代表A初始时有效元素的个数,n代表B中元素的个数,那么n+m才是A的总长度 从A的最后一个位置开始,设为cur,分别 ...
- leetcode题解:Search for a Range (已排序数组范围查找)
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode 题解 | 面试题57 - II. 和为s的连续正数序列
题目描述 面试题57 - II. 和为s的连续正数序列 难度简单37收藏分享切换为英文关注反馈 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内 ...
- leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- LeetCode OJ:Merge Sorted Array(合并排序的数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 深入浅出数据结构C语言版(21)——合并排序
在讲解合并排序之前,我们先来想一想这样一个问题如何解决: 有两个数组A和B,它们都已各自按照从小到大的顺序排好了数据,现在我们要把它们合并为一个数组C,且要求C也是按从小到大的顺序排好,请问该怎么做? ...
- [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)
##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...
随机推荐
- is,数据类型补充,set,深浅拷贝
十二.基础数据类型补充: 1.join 可以把列表变成字符串. s = ‘abc’ s1 = s.join(‘非常可乐’) print(s1) #非abc常abc可abc乐abc s = " ...
- Java复习(五)接口与多态
5.1接口 允许创建者规定方法的基本形式:方法名.参数列表以及返回类型,但不规定方法主体. 也可以包含基本数据类型的数据成员,但他们都默认为static和final 声明格式为 [接口修饰符]inte ...
- BinarySearchTree(二叉搜索树)原理及C++代码实现
BST是一类用途极广的数据结构.它有如下性质:设x是二叉搜索树内的一个结点.如果y是x左子树中的一个结点,那么y.key<=x.key.如果y是x右子树中的一个结点,那么y.key>=x. ...
- Java分层架构的使用规则
原文章引用地址:http://blog.csdn.net/ygzk123/article/details/7816511 三层结构的程序不是说把项目分成DAL, BLL, WebUI三个模块就叫三层了 ...
- diary-2019.9.16
It has been observed by various scientists and nutritionists that it is better to have smaller and a ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
- 项目部署篇之二——linux下安装jdk1.8
1.下载jdk1.8 百度云下载后,直接通过xftp拖到你想放的目录下就行了,实在方便 链接:https://pan.baidu.com/s/1hQl0_3owT776lRO9mHSbXA 提取码:2 ...
- [LC] 1099. Two Sum Less Than K
Given an array A of integers and integer K, return the maximum S such that there exists i < j wit ...
- mysql配置白名单
1. 测试是否允许远程连接 $ telnet 192.168.1.8 3306 host 192.168.1.4 is not allowed to connect to this mysql ser ...
- deeplearning.ai 卷积神经网络 Week 3 目标检测
本周的主题是对象检测(object detection):不但需要检测出物体(image classification),还要能定位出在图片的具体位置(classification with loca ...