【Merge Sorted Array】cpp
题目:
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 i1 = m-, i2 = n-, i = m+n-;
while ( i1>= && i2>= ){
if ( nums1[i1]<nums2[i2] ){
nums1[i--] = nums2[i2--];
}
else{
nums1[i--] = nums1[i1--];
}
}
while (i2>=) nums1[i--] = nums2[i2--];
}
};
tips:
双指针,从后往前扫描。
如果最后i1大于等于0则不用处理了;如果最后i2大于等于0,再处理一下。记住。
============================================
第二次过这道题,想起来双指针从后往前扫描了,细节没有记清。改了两次才AC。
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i1 = m-;
int i2 = n-;
int i = m+n-;
while ( i1>= && i2>= )
{
if ( nums1[i1]>nums2[i2] )
{
nums1[i--] = nums1[i1--];
}
else
{
nums1[i--] = nums2[i2--];
}
}
while ( i2>= ) nums1[i--] = nums2[i2--];
}
};
【Merge Sorted Array】cpp的更多相关文章
- leetcode 【 Merge Sorted Array 】python 实现
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【Remove Duplicates from Sorted Array】cpp
题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- 43. Merge Sorted Array && LRU Cache
Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...
随机推荐
- nginx负载均衡配置一(反向代理)
一.前提 1:系统linux(centos) 2:nginx代理服务器(web:192.168.1.10 proxy.abc.com) 3:nginx后台服务器(web1:192.168.1.11 ...
- Mysql的ssl主从复制+半同步主从复制
Mysql的ssl主从复制+半同步主从复制 准备工作 1.主从服务器时间同步 [root@localhost ~]# crontab -e */30 * * * * /usr/sbin/ntpdate ...
- IOS基础——alloc、init和new方法
alloc:分配内存. init:初始化. new:代替上面两个函数:分配内存,并且初始化. 注意: 1.在实际开发中很少会用到new,一般创建对象时我们一般是 [[className alloc]i ...
- Dev的DocumentManager 相关问题
1.改变DocumentManager包含的窗体的排列方式 if (this.documentManager1.View.Type != ViewType.NativeMdi) { this.docu ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- Service(一)----->简单计算
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- SQLite中命令行程序(CLP)的使用
SQLite CLP是使用和管理SQLite数据库最常用的工具.它在所有平台上的操作方式相同.CLP其实是两个程序,它可以运行在Shell模式下以交互的方式执行查询操作,也可以运行在命令行模式下完成各 ...
- 使用WIF实现单点登录Part IV —— 常见问题
InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException (有关详细 ...
- LinqToSql中使用事务(2)
原文地址:http://www.cnblogs.com/blusehuang/archive/2007/07/16/819677.html
- 【转载】alter table move 和 alter table shrink space的区别
move 和shrink 的共同点1.收缩段2.消除部分行迁移3.消除空间碎片4.使数据更紧密 shrink 语法: alter table TABLE_NAME shrink space [com ...