题目

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 nrespectively.

代码:oj测试通过 Runtime: 58 ms

 class Solution:
# @param A a list of integers
# @param m an integer, length of A
# @param B a list of integers
# @param n an integer, length of B
# @return nothing
def merge(self, A, m, B, n): index_total = m + n - 1
indexA = m-1
indexB = n-1 while indexA >= 0 and indexB >= 0 :
if A[indexA] > B[indexB]:
A[index_total] = A[indexA]
indexA -= 1
else:
A[index_total] = B[indexB]
indexB -= 1
index_total -= 1 if indexA >= 0:
while indexA >= 0:
A[index_total] = A[indexA]
indexA -= 1
index_total -= 1
else:
while indexB >= 0:
A[index_total] = B[indexB]
indexB -= 1
index_total -= 1

思路

采用最基础的两路归并思想。针对A B两个数组维护两个指针。

这里用到一个技巧是:从后往前遍历。

这样可以不用再开辟一个m+n空间的数组。

leetcode 【 Merge Sorted Array 】python 实现的更多相关文章

  1. [leetcode]Merge Sorted Array @ Python

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

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

  3. [Leetcode] Merge Sorted Array (C++)

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

  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 tha ...

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

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

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

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

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

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

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

  9. 【LeetCode练习题】Merge Sorted Array

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

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

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

随机推荐

  1. Cocos2d-x v3.1 坐标系统(五)

    Cocos2d-x v3.1 坐标系统(五) 为了能够更好的布局以及了解对象所在的位置,我们必须对Cocos2d-x中的坐标有详细的了解,本篇文章主要就是了解Cocos中用到的坐标系统.学过数学的人都 ...

  2. meterpreter > screenshot

    meterpreter > screenshotScreenshot saved to: /opt/metasploit/msf3/wLafQYhx.jpegmeterpreter > / ...

  3. Android商城开发系列(十一)—— 首页秒杀布局实现

    首页秒杀布局如下图: 布局使用的是LinearLayout和RecyclerView去实现,新建seckkill_item.xml,代码如下所示: <?xml version="1.0 ...

  4. hihoCoder hiho一下 第十二周 #1055 : 刷油漆 (树上DP)

    思路: 只能刷部分节点数m,总节点数n.如果m>=n那么就可以全刷了,那就不用任何算法了.如果m<n那么就要有取舍了.用DP思路,记录下每个节点如果获得到1~m个选择所能获得的最大权值.这 ...

  5. IFEO 映像文件劫持

    “映像劫持”,也被称为“IFEO”(Image File Execution Options) 映像劫持的根本就是被恶意篡改了注册表HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  6. 流媒体 5——MPEG声音

    1. 听觉系统的感知特性: MPEG声音的数据压缩和编码不是依据波形本身的相关性和模拟人的发音器官的特性,而是利用人的听觉系统的特性来达到压缩声音数据的目的,这种压缩编码称为感知声音编码. 许多科学工 ...

  7. hdu-1879 继续畅通工程---确定部分边的MST

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1879 题目大意: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的 ...

  8. oc语言特性

    It’s a superset of the C programming language and provides object-oriented capabilities and a dynami ...

  9. gearmand 编译 could not find gperf

    安装步骤: #wget https://launchpad.net/gearmand/1.2/1.1.8/+download/gearmand-1.1.8.tar.gz #tar zxvf gearm ...

  10. axios向后端请求解决跨域问题

    我要向后端的请求的url是 http://192.168.3.25/ productInfo/insert 我是先用niginx转成localhost:8081 找conf/ nginx.conf , ...