分析
难度 易
来源
题目
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
- The number of elements initialized in nums1 and nums2 are m and n respectively.
- You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
解答
Runtime: 3 ms, faster than 99.99% of Java online submissions for Merge Sorted Array.
1 package LeetCode; 2 3 public class L88_MergeSortedArray { 4 public void merge(int[] nums1, int m, int[] nums2, int n) { 5 int len=m+n; 6 int count=0;//从后往前按顺序插入,已确定位置数字数目 7 int i=0,j=0;//i j分别为数组nums1 nums2上的游标 8 while(i=nums2[n-1-j]){10 nums1[len-1-count]=nums1[m-1-i];11 i++;12 count++;13 }14 else{15 nums1[len-1-count]=nums2[n-1-j];16 j++;17 count++;18 }19 }20 if(i
posted on 2018-10-29 22:29 阅读( ...) 评论( ...)