88. Merge Sorted Array【easy】
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】的更多相关文章
- 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88 Merge Sorted Array(归并排序Easy)
题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中 class Solution { public: void merge(vector<int>& nums ...
- 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 ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- 【树状数组】Codeforces Round #755 D. PolandBall and Polygon
http://codeforces.com/problemset/problem/755/D 每次新画一条对角线的时候,考虑其跨越了几条原有的对角线. 可以用树状数组区间修改点查询来维护多边形的顶点. ...
- boost 1.57 vs2013 编译
启动vs2013中的命令行注意区分32/64, 进入boost目录, 再次运行 bootstrap.bat 编译: bjam.exe stage --toolset=msvc-12.0 --sta ...
- java读写文件及保留指定位小数
1)先上代码: public static void main(String[] args)throws IOException{ double[][] B=new double[1043][2102 ...
- net core 踩坑
1.压缩静态文件,要nuget BuildBundlerMinifier 2.DI 自动注入 添加了两个类,才能完成 3.所有静态文件必须放在wwwroot下,不然访问不到 4.ajax 提交Typ ...
- TSQLDBServerHttpApi使用工作线程池
TSQLDBServerHttpApi使用工作线程池 TSQLDBServerHttpApi创建时,默认是使用单线程模式,且只使用一个数据库连接,服务端要应对众多的客户端只靠一个工作线程(主线程)和一 ...
- ios 如何获得系统时间和日期
iphone 如何获得系统时间和日期 代码如下: #import <time.h> 1.获得当前的系统时间和日期 //获得系统时间 NSDate * senddate=[NSDate d ...
- SqlServer_合并多个递归查询数据(CTE)
该方法在数据量过大时,效率过低,可参考hierarchyid字段实现(Sqlserver 2008) 优点:效率较高 缺点:需要不断维护数据,对现有业务有一定影响 参考:http://www.cnbl ...
- nhibernate change connection
http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring http://stackoverflow ...
- [Ubuntu Setup] Ubuntu 14.10 LTS 中文输入法的安装
from : http://www.cnblogs.com/zhj5chengfeng/archive/2013/06/23/3150620.html http://xboot.org/thread- ...
- CentOS release 6.6 (Final)如何安装firefox和chromium
一.firefox的安装: 1. 安装remi源 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8 ...