Merge k Sorted Arrays

原创 2016年05月30日 21:45:07

此题是自己的一个学习过程,真是第一次用PriortyQueue,如何定义一个class并在当中实现Comparable接口,如何用PriortyQueue的思维去处理问题。

在定义ArrayContainer中,一定要明确的是index到底代表什么,是自身数组的当前下标,而与[][]的循环无关,所以当中的第一个错误不应该。第二个错误,是告诉我们要处理边界条件。

整个思路参考:点击打开链接

/**
     * @param arrays k sorted integer arrays
     * @return a sorted array
     */
    public List<Integer> mergekSortedArrays(int[][] arrays) {
        // Write your code here
        List<Integer> results = new ArrayList<>();
        if (arrays == null || arrays.length == 0) {
            return results;
        }
        PriorityQueue<ArrayContainer> pq = new PriorityQueue<>();
        for (int i = 0; i < arrays.length; i++) {
            ////2 
            if (arrays[i].length != 0) {
            ////2
                //1 ArrayContainer ac = new ArrayContainer(arrays[i], i);
                ArrayContainer ac = new ArrayContainer(arrays[i], 0);
                pq.add(ac);
            }
        }
        while (!pq.isEmpty()) {
            ArrayContainer ac = pq.poll();
            results.add(ac.array[ac.index++]);
            if (ac.index < ac.array.length) {
                pq.add(new ArrayContainer(ac.array, ac.index));
            }
        }
        return results;
    }
    
    private class ArrayContainer implements Comparable<ArrayContainer> {
        int []array;
        int index;
        public ArrayContainer (int []array, int index) {
            this.array = array;
            this.index = index;
        }
        
        @Override 
        public int compareTo(ArrayContainer o) {
            return this.array[index] - o.array[o.index];
        }
    } 


[leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数

先用图片讲下大概的原理: 下面是实现的代码: 代码基本上是按照leetcode中的某位大神的c++代码改写为java的 地址为:https://leetcode.com/discuss/30...

leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值

问题 Median of Two Sorted ArraysThere are two sorted arrays A and B of size m and n respectively. Find...

4. Median of Two Sorted Arrays 典型的二分,分治算法, 另 第k小的O(lgk)解法

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the tw...

[leetcode]median of two sorted arrays【寻找第k小的数问题】

Median of Two Sorted ArraysThere are two sorted arrays nums1 and nums2 of size m and n respectively....

LeetCode4. Median of Two Sorted Arrays(寻找第k小数:分治O(log(n+m)))

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 ...

Find the top k sums of two sorted arrays

Given two arrays sorted in ascending order, we want to find the kth largest sum such that one elemen...

23. Merge k Sorted Lists等题 第二周解题报告

原题Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity....
  • Mr_Zing
  • Mr_Zing
  • 2016年09月11日 23:47
  • 565

[LeetCode] Merge k Sorted Lists

Merge k Sorted Lists  Merge k sorted linked lists and return it as one sorted list. Analyze and...

23. Merge k Sorted Lists(java实现)

23. Merge k Sorted Lists(java实现)

[LeetCode]Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Merge k Sorted Arrays
举报原因:
原因补充:

(最多只允许输入30个字)