From 230234c0320406cf4d3a97bc4ce923fa4076376f Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 29 Nov 2020 22:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=B8=A4=E4=B8=AA=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leetcode/array/MergeSortedArrays.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/leetcode/array/MergeSortedArrays.java diff --git a/src/main/java/com/leetcode/array/MergeSortedArrays.java b/src/main/java/com/leetcode/array/MergeSortedArrays.java new file mode 100644 index 0000000..bc19eae --- /dev/null +++ b/src/main/java/com/leetcode/array/MergeSortedArrays.java @@ -0,0 +1,49 @@ +package com.leetcode.array; + +import java.util.Arrays; + +/** + * 合并两个有序数组 +给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。 + +说明: + +初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。 +你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。 + +示例: + +输入: +nums1 = [1,2,3,0,0,0], m = 3 +nums2 = [2,5,6], n = 3 + +输出:[1,2,2,3,5,6] + +作者:力扣 (LeetCode) +链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xmi2l7/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + */ +public class MergeSortedArrays { + + public void merge(int[] nums1, int m, int[] nums2, int n) { + for (int i = 0;i < n; i++ ) { + nums1[m + i] = nums2[i]; + } + Arrays.sort(nums1); + } + + public static void main(String[] args) { + MergeSortedArrays mArrays = new MergeSortedArrays(); + int[] nums1 = {1,2,3,0,0,0}; + int[] nums2 = {2,5,6}; + int m = 3; + int n = 3; + mArrays.merge(nums1, m, nums2, n); + for (int i = 0; i < nums1.length; i++) { + System.out.print(nums1[i] + ","); + } + System.out.println(); + } + +}