我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。

题目:

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.

Tag:

Array; Two Pointers

体会:

这个小题,初看很简单,但是实现起来还是有些小地方需要特别注意。首先一个特别的地方就是在A里面merge B。如果是像原始merge算法从头开始merge的话,就可能会出现需要插入操作的地方。(比如需要把B[1]插入到A[2]后)。这时就需要把A[3]到A[last]全部往后移动一位。因为不是链表,明显不合适。但是题目又要求merge到A, 那怎么办呢,解决办法就是从A的屁股上开始merge,就是从大数到小数开始merge。

具体执行的时候,我们的目标就是把B merge 完,即int j = n - 1,  while (j >= 0)就继续执行。

那什么时候选择来自B的元素呢,这里面要注意A的边界情况,即A的元素已经都用完了。所以会有两种情况选择来自B的元素:

一是A的元素用完了 (i.e. i < 0),无条件选择B的;二是当A没完时,有B[j] > A[i]。

 class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i = m - ;
int j = n - ;
int k = m + n - ;
// when B is all merged into A, job done
while (j >= ) {
// merge
if (i < || B[j] > A[i]) {
// when A is done or A is less than B, choose B
A[k] = B[j];
j--;
} else {
A[k] = A[i];
i--;
}
k--;
}
} };

[Leetcode] Merge Sorted Array (C++)的更多相关文章

  1. [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 ...

  2. [leetcode]Merge Sorted Array @ Python

    原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...

  3. [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 ...

  4. [Leetcode] merge sorted array 合并数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  5. LeetCode Merge Sorted Array 合并已排序的数组

    void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...

  6. leetcode - Merge Sorted Array (run time beats 100.00% of cpp submissions.)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  7. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  8. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  9. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

随机推荐

  1. nginx入门手册(一)

    1.nginx进程: nginx会启动多个进程: 一个主进程Master. 几个工作进程worker. 缓存加载器进程 缓存管理器进程 master主要工作: 1. 读取并验正配置信息: 2. 创建. ...

  2. jQuery1.9(辅助函数)学习之—— jQuery.param( obj ); 编辑

    jQuery.param( obj );  返回一个String 描述: 创建一个数组或对象序列化的的字符串,适用于一个URL 地址查询字符串或Ajax请求. jQuery.param( obj ); ...

  3. 移动平台中 meta 标签的使用

    一.meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). 1.http-equiv 属性的 Content-Type 值(显示字符集的设定) 说明:设定页面 ...

  4. Net线程池设计

    Net线程池设计 功能描述: 支持创建多个线程池,并统一管理 支持不同线程池的容量控制,以及最少活动线程的设置 支持不同线程池中活动线程的闲时设置,即线程空闲时间到期后即自动被回收 结构设计: Thr ...

  5. matlab读取多幅图片,并对读取的图片降采样和双三次插值

    clear all clc im = {}; %%创建字典im以保存读取的图片 dis = dir('C:\Users\KCl\Documents\MATLAB\SRCNN\Set5\*.bmp'); ...

  6. QuickTime 专业版 pro 注册码

    打开QuickTime Player下拉编辑菜单--选偏好设置--注册 Name: Dawn M Fredette Key: 4UJ2-5NLF-HFFA-9JW3-X2KV 重新启动 QuickTi ...

  7. Manacher马拉车

    俗话说:摩托再好,不如骡拉啊(好像不是骡) Manacher就是O(N)计算最长回文子串的算法. 其中我们需要在0位置加入字符“$",然后原字符串中每两个字符加入一个"#" ...

  8. HDU 4274 Spy's Work (树 DFS)

    给定N个点,每个点都有一个唯一的前驱结点(点1为大boss),每个点的实际权值是子节点的求和值.现在给出某些点的权值的估算(> , = , < ),问这些估算是否会有冲突,现在保证每个点的 ...

  9. LeetCode 191. Number of 1 Bits Question

    题意:给你一个整数,计算该整数的二进制形式里有多少个“1”.比如6(110),就有2个“1”. 一开始我就把数字n不断右移,然后判定最右位是否为1,是就cnt++,否则就继续右移直到n为0. 可是题目 ...

  10. java之文件夹

    1.文件夹的创建 code: package com.test; import java.io.File; public class Folder_test { public static void ...