[LeetCode]题解(python):088 Merge Sorted Array

题目来源



https://leetcode.com/problems/merge-sorted-array/

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

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1and nums2 are m and n respectively.



题意分析



Input:

  :type nums1: List[int]
  :type m: int
  :type nums2: List[int]
  :type n: int

Output:

:rtype: void Do not return anything, modify nums1 in-place instead.

Conditions:合并两个sorted的list,注意是将nums2合并进nums1当中,结果也必须是sorted,不返回任何元素。



题目思路

注意到nums1是有足够的空间存储m+n个元素的,所以直接对nums1进行操作就好,选择从数值较大的位置开始选择元素。

备注:如果n小于0了,就不需要继续合并了。



AC代码(Python)


 1 class Solution(object):
 2     def merge(self, nums1, m, nums2, n):
 3         """
 4         :type nums1: List[int]
 5         :type m: int
 6         :type nums2: List[int]
 7         :type n: int
 8         :rtype: void Do not return anything, modify nums1 in-place instead.
 9         """
10         all = m + n - 1
11         m = m - 1
12         n = n - 1
13         while m >= 0 and n >= 0:
14             if nums1[m] > nums2[n]:
15                 nums1[all] = nums1[m]
16                 m = m - 1
17                 all = all - 1
18             else:
19                 nums1[all] = nums2[n]
20                 n = n - 1
21                 all = all - 1
22         while n >= 0:
23             nums1[all] = nums2[n]
24             all = all - 1
25             n = n - 1
26
27
28         
时间: 2024-08-01 20:05:12

[LeetCode]题解(python):088 Merge Sorted Array的相关文章

Java for LeetCode 088 Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: 使用一个nums1Copy把nums1的前m个元素拷贝出来,然后把nums1Copy和nums2放进nums1中,JAVA实现如下: public void merge(int[] nums1, int m, int[] nums2, int n) { int[] nums1Copy=new int

LeetCode(88)Merge Sorted Array

题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

LeetCode(40)-Merge Sorted Array

听到初爱有感 开头啰嗦两句.刚在做算法题目的时候.听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己如今研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is g

088. Merge Sorted Array

1 void merge(vector<int>& A, vector<int>& B, int m, int n) 2 { 3 int indexA = m - 1, indexB = n - 1, index = m + n - 1; 4 while (index >= 0) { 5 while (indexA >= 0 && indexB >= 0) { 6 if (A[indexA] >= B[indexB]) { 7

leetcode学习笔记:Merge Sorted Array

一.题目描述 二.解题技巧 这道题不存在复杂的分析过程和边界条件.如果单纯得考虑从小到大地将两个数组进行合并的话,每次在num1中插入一个数的话,需要将后面的元素都向后移动一位,这样,整个处理过程的时间复杂度为O(m*n). 由于两个数组的元素的个数是知道的,同时,合并后的数组也是递增排序的,也就是说,排序之后的数组的最大值是放在最后面的,因此,我们可以从后往前遍历,也就是将最大值放在第一个数组的m+n-1位置,然后将次最大值放在m+n-2位置,依次类推,这样在将元素放置到合适位置的时候,就不需

leetcode第88题-Merge Sorted Array

题目的意思:合并两个有序的数组到第一个数组中,使合并之后的数组仍然有序,且假设第一个数组有足够的空间. 解题思路:一开始遇到这个题目,我也是不顾一切的从头开始遍历,结果很麻烦,总是有一两个位置走不对,数组并不像链表那样插入一个节点这么简单,我们要解决的就是插入节点的位置问题.所幸,上网查了一下其他人的做法,突然想到了可以从后往前遍历,因为每个数组都是有序的,比较两个数组的最后一个位置的元素大小就可以确定该元素的最终位置,最后就利用这个想法AC了这道题. #include<stdio.h> #i

【LeetCode 88_数组】Merge Sorted Array

1 void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 2 { 3 int i = m - 1; 4 int j = n - 1; 5 int k = m + n - 1; 6 while (i >=0 && j >= 0) { 7 if (nums1[i] > nums2[j]) 8 nums1[k--] = nums1[i--]; 9 else 10

归并有序数组--Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/ Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n)

leetcode 题解: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 ar