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

合并两个有序的数组

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();
        }
    }
}
全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务