NC88 寻找第K大、NC33 合并两个排序的链表

 

描述

有一个整数数组,请你根据快速排序的思路,找出数组中第 k 大的数。

给定一个整数数组 a ,同时给定它的大小n和要找的 k ,请返回第 k 大的数(包括重复的元素,不用去重),保证答案存在。

要求:时间复杂度 O(nlogn)O(nlogn),空间复杂度 O(1)O(1)

数据范围:0≤n≤10000≤n≤1000, 1≤K≤n1≤K≤n,数组中每个元素满足 0≤val≤100000000≤val≤10000000

示例1

输入:

[1,3,5,2,2],5,3

返回值:

2

示例2

输入:

[10,10,9,9,8,7,5,6,4,3,4,2],12,3

返回值:

9

说明:

去重后的第3大是8,但本题要求包含重复的元素,不用去重,所以输出9        
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param a int整型一维数组 
     * @param n int整型 
     * @param K int整型 
     * @return int整型
     */
    public int findKth (int[] a, int n, int K) {
        // write code here
        if(n == 0) return 0;
        int pivot = a[0];
        List<Integer> small = new ArrayList<Integer>();
        List<Integer> big = new ArrayList<Integer>();
        for(int i = 1; i < n; i++) {
            if(a[i] > pivot) big.add(a[i]);
            if(a[i] <= pivot) small.add(a[i]);
        }

        if(big.size() >= K) pivot = findKth(listToInt(big),big.size(),K);
        if(n - small.size() < K) pivot = findKth(listToInt(small),small.size(),K-1-big.size());
        return pivot;
    }

    public int[] listToInt(List<Integer> list) {
        int[] res = new int[list.size()];
        for(int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}

NC33 合并两个排序的链表

描述

输入两个递增的链表,单个链表的长度为n,合并这两个链表并使新链表中的节点仍然是递增排序的。

数据范围: 0≤n≤10000≤n≤1000,−1000≤节点值≤1000−1000≤节点值≤1000
要求:空间复杂度 O(1)O(1),时间复杂度 O(n)O(n)

如输入{1,3,5},{2,4,6}时,合并后的链表为{1,2,3,4,5,6},所以对应的输出为{1,2,3,4,5,6},转换过程如下图所示:

或输入{-1,2,4},{1,3,4}时,合并后的链表为{-1,1,2,3,4,4},所以对应的输出为{-1,1,2,3,4,4},转换过程如下图所示:

示例1

输入:

{1,3,5},{2,4,6}

返回值:

{1,2,3,4,5,6}

示例2

输入:

{},{}

返回值:

{}

示例3

输入:

{-1,2,4},{1,3,4}

返回值:

{-1,1,2,3,4,4}
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead1 ListNode类 
     * @param pHead2 ListNode类 
     * @return ListNode类
     */
    public ListNode Merge (ListNode pHead1, ListNode pHead2) {
        // write code here
        ListNode res = new ListNode(0);
        ListNode dummy = res;
        if(pHead1 == null || pHead2 == null) return pHead1 == null ? pHead2 : pHead1;
        while(pHead1 != null && pHead2 != null) {
            if(pHead1.val > pHead2.val) {
                res.next = pHead2;
                pHead2 = pHead2.next;
            } else {
                res.next = pHead1;
                pHead1 = pHead1.next;
            }
            res = res.next;
        }
        if(pHead1 != null || pHead2 != null) res.next = pHead1 == null ? pHead2 : pHead1;
        return dummy.next;
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值