您的位置:首页 > 其它

Merge Sorted Array 【leetcode】

2015-07-09 09:47 330 查看

Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.

Note:

You may assume that nums1 has enough space (size that is greater or equal tom +
n) to hold additional elements from nums2. The number of elements initialized innums1 and
nums2 are m and n respectively.

读题才是最重要的,我一开始并没有看懂需要排序

public class Solution {

    public void merge(int[] nums1, int m, int[] nums2, int n) {

       int index2=0;

        int index1=m;

        while(index2!=n){

         nums1[index1]=nums2[index2];

         index1++;

         index2++;

        }

        for(int i = 0 ; i < nums1.length ; i ++) { 

         for(int j = i +1 ; j < nums1.length ; j ++) {  

          if(nums1[i] > nums1[j]) {   

           int temp = nums1[i];   

           nums1[i] = nums1[j];   

           nums1[j] = temp;  

           } 

          }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: