zoukankan      html  css  js  c++  java
  • 求两个有序数组的第k大的数(默认两有序数组都为从小到大)

    一、两有序数组第k大的数

        public static int topk(int[] a,int[] b,int alen, int blen, int k) {
            if(alen==0) {
                return b[b.length-blen+k-1];
            }
            if(blen==0) {
                return a[a.length-alen+k-1];
            }
            if(k==1) {
                return a[a.length-alen]<b[b.length-blen]?a[a.length-alen]:b[b.length-blen];
            }
            int amin = alen>k/2?k/2:alen;
            int bmin = blen>k/2?k/2:blen;
            if(a[a.length-alen+amin-1] < b[b.length-blen+bmin-1]) {
                k = k - amin;
                alen = alen - amin;
                return topk(a,b,alen,blen,k);
            }else if(a[a.length-alen+amin-1] > b[b.length-blen+bmin-1]){
                k = k - bmin;
                blen = blen - bmin;
                return topk(a,b,alen,blen,k);
            }else {
                k = k - amin - bmin;
                if(k == 0) {
                    return a[a.length-alen+amin-1];
                }
                alen = alen - amin;
                blen = blen - bmin;
                return topk(a,b,alen,blen,k);
            }
        }

     下面这种可能好理解,但是性能就会比较差

        public static int topk(int[] a,int[] b, int k) {
            int alen = a.length;
            int blen = b.length;
            if(alen==0) {
                return b[k-1];
            }
            if(blen==0) {
                return a[k-1];
            }
            if(k==1) {
                return a[0]<b[0]?a[0]:b[0];
            }
            int amin = alen>k/2?k/2:alen;
            int bmin = blen>k/2?k/2:blen;
            int[] newa = null;
            int[] newb = null;
            if(a[amin-1] < b[bmin-1]) {
                k = k - amin;
                alen = alen - amin;
                newa = Arrays.copyOfRange(a, a.length-alen, a.length);
                newb = Arrays.copyOfRange(b, b.length-blen, b.length);
                return topk(newa,newb,k);
            }else if(a[amin-1] > b[bmin-1]){
                k = k - bmin;
                blen = blen - bmin;
                newa = Arrays.copyOfRange(a, a.length-alen, a.length);
                newb = Arrays.copyOfRange(b, b.length-blen, b.length);
                return topk(newa,newb,k);
            }else {
                k = k - amin - bmin;
                if(k == 0) {
                    return a[amin-1];
                }
                alen = alen - amin;
                blen = blen - bmin;
                newa = Arrays.copyOfRange(a, a.length-alen, a.length);
                newb = Arrays.copyOfRange(b, b.length-blen, b.length);
                return topk(newa,newb,k);
            }
        }
  • 相关阅读:
    深度学习网络调试技巧
    深度学习网络调参技巧
    用深度学习解决大规模文本分类问题
    [译] 理解 LSTM 网络
    一文学会用 Tensorflow 搭建神经网络
    tensorflow实现基于LSTM的文本分类方法
    用keras实现lstm 利用Keras下的LSTM进行情感分析
    TensorFlow练习7: 基于RNN生成古诗词
    【深度学习】文本生成
    Leetcode--easy系列5
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11024139.html
Copyright © 2011-2022 走看看