|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Seawolf 于 2019-9-2 07:01 编辑
- 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]
复制代码
- class Solution {
- public void merge(int[] nums1, int m, int[] nums2, int n) {
-
- if(n == 0) return;
-
- if(m == 0){
-
- for(int i = 0; i< n; i++){
-
- nums1[i] = nums2[i];
- }
- }
-
- int count1 = 0, count2= 0, count3= 0;
-
- int[] arr = new int[m+n];
-
- while(count1 != (m+n)){
-
- if(count3 < n && (count2 == m || nums1[count2] >= nums2[count3])){
-
- arr[count1++] = nums2[count3++];
- }
- else if(count2 < m && (count3 == n || nums1[count2] < nums2[count3])){
-
- arr[count1++] = nums1[count2++];
- }
-
- }
-
- for(int i = 0; i< arr.length; i++){
-
- nums1[i] = arr[i];
- }
-
- }
- }
复制代码 |
|