题目描述

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.

解题思路:

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(void)
def merge(self, A, m, B, n):
posa = m - 1
posb = n - 1
for i in range(m+n-1,-1,-1):
if posa < 0:
A[i] = B[posb]
posb -= 1
elif posb < 0:
return
elif A[posa] > B[posb]:
A[i] = A[posa]
posa -= 1
else:
A[i] = B[posb]
posb -= 1

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

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

  2. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  3. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  4. 【Leetcode】【Easy】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】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  6. 【leetcode】Convert Sorted Array to Binary Search Tree (easy)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...

  7. 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...

  8. 【LeeCode88】Merge Sorted Array★

    1.题目描述: 2.解题思路: 题意:两个由整数构成的有序数组nums1和nums2,合并nums2到nums1,使之成为一个有序数组.注意,假设数组nums1有足够的空间存储nums1和nums2的 ...

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. zabbix安装

    在服务器10.128.17.136上安装 1.安装mysql \# yum -y install mysql mysql-server mysql-devel MySQL 配置文件/etc/my.cn ...

  2. Java中接口的实现问题

    1.Java 接口的访问权限 interface A{}//接口A包访问权限 public interface A{}//接口A公有访问 interface A{ void function1(): ...

  3. Quartz.NET总结(四)Quartz 远程调度

    前面篇已经介绍了Quartz.NET的配置,使用和Cron表达式表达式的写法.基本上后台的定时任务的定时执行已经完成,并能正确的按照执行计划,执行相关的job . 然后,如果任务需要更新,停止某个任务 ...

  4. 现代软件工程作业 第二章 Github的使用

    Github的使用 创建团队 Github首页点击Create Orginazation,出现如下界面: 填写相关信息,邀请团队成员: 点击确认,创建团队完成,界面如下: 创建新的版本库 点击Crea ...

  5. webservice 测试窗体只能用于来自本地计算机的请求

    Question: WebService部署成站点之后,如果在本地测试webservice可以运行,在远程却显示“测试窗体只能用于来自本地计算机的请求”或者"The test form is ...

  6. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

  7. 疑问,关于win64无法使用debug的问题

    学习汇编语言的过程中,无法使用debug.exe,怎么办? 使用这篇文章中的解决方案 http://www.cnblogs.com/xuepeng0521/p/3661598.html 结果调试程序的 ...

  8. kali安装vmtools问题

    切记使用此法,一定要确保kali没有装过vmware workstation自带的vmware_tools,不然要卸载之后才能使用.我当初就是安装了后使用此法,不能成功,卸载也不行,导致完全重装 安装 ...

  9. nexus的使用

    一.在百度网盘或官网下载nexus,并部署.   注意修改: https://repository.apache.org/content/repositories/releases/    二.下载m ...

  10. Integer与int的区别

    简述:int与Integer的区别: 对于它们,我们可能只是知道简单的区别.Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null.但是他们之间真的仅仅只有这些区别 ...