【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: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
解法一:从前往后,小的排最前。每次插入一个B,A需要整体后移。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int indA = ;
int indB = ;
int shift = ;
while(indA < m+shift && indB < n)
{
if(A[indA] <= B[indB])
indA++;
else
{
for(int i = m-+shift; i >= indA; i --)
A[i+] = A[i];
shift ++;
A[indA++] = B[indB++];
}
}
if(indA >= m+shift)
{
while(indB < n)
A[indA++] = B[indB++];
}
}
};

解法二:从后往前,大的排最后。不需要多余的移动。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int indA = m-;
int indB = n-;
for(int i = m+n-; i >= ; i --)
{
if(indA < )
{// A finished
A[i] = B[indB --];
}
else if(indB < )
{// B finished
A[i] = A[indA --];
}
else
{
if(A[indA] >= B[indB])
A[i] = A[indA --];
else
A[i] = B[indB --];
}
}
}
};

【LeetCode】88. Merge Sorted Array (2 solutions)的更多相关文章
- 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...
- 【LeetCode】88 - Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- 【一天一道LeetCode】#88. Merge Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【easy】88. Merge Sorted Array 合并两个有序数组
合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...
- LeetCode OJ 88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- Leetcode No.88 Merge Sorted Array(c++实现)
1. 题目 1.1 英文题目 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【leetcode❤python】 88. Merge Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def merge(self, nums1, m, nums2, n): "& ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree
Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...
随机推荐
- codeforces 560 C Gerald's Hexagon
神精度--------这都能过.随便算就好了,根本不用操心 就是把六边形补全成三角形.然后去掉补的三个三角形,然后面积除以边长1的三角形的面积就可以.... #include<map> # ...
- 解决报错"Your security system have blocked an application with expired or not yet valid certificate from running"
方法如下: Go to Control Panel Java in the Security tab click the "Edit Site List-" button clic ...
- 同时启动多个Tomcat服务器
以下步骤能够同时启动两个tomcat:1.特别要注意:不要设置CATALINA_HOME 2.分别修改安装目录下的conf子目录中的server.xml文件: a.修改http访问端口(默认为8080 ...
- 你应该知道的30个jQuery代码开发技巧
1. 创建一个嵌套的过滤器 .filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素 2. 重用你的元素查询 var ...
- 使用SqlServer中的float类型时发现的问题
在做项目中,使用了float类型来定义一些列,如:Price,但是发现了很多问题1.当值的位数大于6位是float型再转varchar型的时候会变为科学技术法显示 此时只好将float型转换成n ...
- c++call back
#include "stdafx.h" struct A; typedef void(A::*MemFuncPtr) (int* e); class A { int a; }; c ...
- cocos2dx游戏存储举例及其注意事项
今天白白跟大家分享一下cocos2dx中游戏的存储及需要注意的事项 cocos2dx中自带了存储类:CCUserDefault ,倘若需要存储的数据量教大的话,建议使用数据库来存储 现在先给大家看一下 ...
- Cognos让指定用户不具有删除内容的权限
为了方便用户使用Cognos,现在很多对权限要求不够严格的用户就想到了可以让用户实现匿名登陆,即不登陆系统即可实现访问报表,当然这也仅仅是按照客户的需求,我个人认为一个安全性的数据平台还是需要对登陆. ...
- mobile移动网页开发常用代码模板
index.html <!DOCTYPE HTML> <html> <head> <!--申明当前页面的编码集--> <meta http-equ ...
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检測的測试代码
Tadas Baltrusaitis的OpenFace是一个开源的面部行为分析工具.它的源代码能够从 https://github.com/TadasBaltrusaitis/OpenFace 下载. ...