好,这个是今天的,好晚了还有别的代码要改,所以写个简单的数组。
题目
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1中,使得 num1 成为一个有序数组。
注意:
你可以假设 nums1有足够的空间(空间大小大于或等于m + n)来保存 nums2 中的元素。在 nums1 和 nums2 中初始化的元素的数量分别是 m 和 n。
分析
两个有序数组结合一下,嗯。。。按顺序排就是了
代码
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] nums = new int[m+n];
int index = 0;
int i = 0;
int j = 0;
while (i < m && j < n )
if (nums1[i] <= nums2[j])
nums[index++] = nums1[i++];
else
nums[index++] = nums2[j++];
if ( i < m)
while (i<m)
nums[index++] = nums1[i++];
if (j < n)
while (j<n)
nums[index++] = nums2[j++];
System.arraycopy(nums,0,nums1,0,nums.length);
}
}