原题地址:https://oj.leetcode.com/problems/merge-sorted-array/

题意:Given two sorted integer arrays A and B, merge B into A as one sorted array.

解题思路:归并排序的归并这一步的实现,原理很多地方都有。使用一个tmp临时数组进行归并。

代码:

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):
tmp = [0 for i in range(m + n)]
i = 0; j = 0; k = 0
while i < m and j < n:
if A[i] <= B[j]:
tmp[k] = A[i]; i += 1
else:
tmp[k] = B[j]; j += 1
k += 1
if i == m:
while k < m + n:
tmp[k] = B[j]; k += 1; j += 1
else:
while k < m + n:
tmp[k] = A[i]; i += 1; k += 1
for i in range(0, m + n):
A[i] = tmp[i]

[leetcode]Merge Sorted Array @ Python的更多相关文章

  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 (C++)

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

  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. lvs+keepalived 02

    LVS keepalived 高可用负载均衡 环境 IP HOSTNAME Describe 192.168.100.30 lvs01 主负载 192.168.100.31 lvs02 备负载 192 ...

  2. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  3. OPTIMIZE TABLE ipc_analysisdatasyn, ipc_analysisdatatkv,ipc_autoupdateset, ipc_equipmentwaring,ipc_fguid, ipc_receivedata, ipc_senddata, tb_qualitativeanalysis, tb_quantifyresult, tb_quantifyresulthis

    OPTIMIZE TABLE ipc_analysisdatasyn, ipc_analysisdatatkv,ipc_autoupdateset, ipc_equipmentwaring,ipc_f ...

  4. 我所经历的SAP选型[转]

    这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...

  5. Continuous Integration for iOS Apps with Visual Studio Team Services

    原文引用自:https://blog.xamarin.com/continuous-integration-for-ios-apps-with-visual-studio-team-services/ ...

  6. WCF实现多个服务

    本篇体验使用WCF实现2个服务.一个使用HTTP通讯,一个使用TCP通讯. 大致思路是: → 创建WCF服务以及接口,写2个接口→ 实现2个接口→ 为WCF创建一个控制台项目的宿主,配置App.con ...

  7. ASP.NET MVC中商品模块小样

    在前面的几篇文章中,已经在控制台和界面实现了属性值的笛卡尔乘积,这是商品模块中的一个难点.本篇就来实现在ASP.NET MVC4下商品模块的一个小样.与本篇相关的文章包括: 1.ASP.NET MVC ...

  8. Delphi实例分析:远程传输数据和文件

    在Windows操作系统的平台上,WinSock是首选的网络编程接口,用于在网络上传输数据和交换信息,它构成了Windows操作系统进行网络编程的基础.对于编写网络应用程序来说,WinSock是一门非 ...

  9. delphi版本修改PE头源码

    //VC++6外衣 1 OEPCODEFIVE: THEAD = ($55, $8B, $EC, $6A, $FF, $68, $00, $00, $00, $00, $68, $00, $00, $ ...

  10. Idea2018激活破解

    原文:https://www.imsxm.com/2018/07/idea-2018-1-5-crack-patcher.html 最近更新了Intellij IDEA到2018.1.5之后,使用之前 ...