88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

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 initialized in nums1 and nums2 are m and n respectively.

题目:

给定两个排序整数数组A和B,将b合并为一个排序数组。
注意:您可以假设a有足够的空间(大小大于或等于m + n)来保存来自B的附加元素。

分析:

循环递归解决即可,最大的在最后,倒序进行处理

方法1:

 1 public class Solution {
 2     public void merge(int[] nums1, int m, int[] nums2, int n) {
 3        // 特别注意这里的m只是nums1中元素的个数,不是最终第一个数组的长度 不要用m=nums1.length;不然会造成数据越界的报错
 4
 5         int i=m-1,j=n-1,index=m+n-1;
 6
 7         while(i>=0&&j>=0){
 8             if(nums1[i]>nums2[j]){
 9                 //A大就把A的数组放在更后面
10                 nums1[index--]=nums1[i--];
11
12             }
13             else{
14                 nums1[index--]=nums2[j--];
15
16             }
17         }
18         while(i>=0){
19             //A大就把A的数组放在更后面
20             nums1[index--]=nums1[i--];
21         }
22         while(j>=0){
23             nums1[index--]=nums2[j--];
24         }
25
26     }
27 }

方法二:三行代码

public class Solution {
    public void merge(int[] A, int m, int[] B, int n) {
        int i=m-1, j=n-1, k=m+n-1;
        while (i>-1 && j>-1) A[k--]= (A[i]>B[j]) ? A[i--] : B[j--];
        while (j>-1)         A[k--]=B[j--];

    }
}
时间: 2024-10-10 20:15:05

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中的相关文章

LeetCode --- 88. Merge Sorted Array

题目链接: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 init

leetCode 88. Merge Sorted Array 有序数组

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 n

No.88 Merge Sorted Array

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

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

题目: 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 a

[LeetCode] 88. Merge Sorted Array Java

题目: 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 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 initi

【一天一道LeetCode】#88. Merge Sorted Array

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【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 initi

LeetCode 88 Merge Sorted Array(合并排序数组)(*)

翻译 给定两个排序的整型数组nums1和nums2,将nums2合并到nums1成一个排序数组. 批注: 你可以假设nums1中有足够的空间(空间大于或等于m+n)来存放来自nums2的额外元素. nums1和nums2的初始空间分别是m和n. 原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1