题解 | #合并两个有序的数组#

合并两个有序的数组

http://www.nowcoder.com/practice/89865d4375634fc484f3a24b7fe65665

//使用现成的工具,将数组转化为链表,使用Collections里的sort方法,虽然简单,但是占用内存过多
import java.util.*;
public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        //first :put the B array's Element into A array
        for(int i = 0;i<n;i++){
            A[m+i] = B[i];
        }
        //turn A array to a List
        List<Integer>l1 = new ArrayList<>();
        for(int temp:A){
            l1.add(temp);
        }
        Collections.sort(l1);
        Iterator<Integer> it = l1.iterator();
        int i = 0;
        while(it.hasNext()){
            A[i++] = it.next();
        }
    }
}
全部评论

相关推荐

04-03 12:09
東京大学 C++
点赞 评论 收藏
分享
03-16 22:00
武汉大学 C++
幸福的小熊猫想要offer:我阿里投的 c++岗,面试官说自己是做 java 的,c++这辈子才有了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务