题解 | #寻找第K大#

寻找第K大

https://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf

using System;
using System.Collections.Generic;

class Solution {
    SortedDictionary<int, int> sortDic = new SortedDictionary<int, int>();
    public int findKth(List<int> a, int n, int K) {
        // write code here
        if (n == 0 || K > a.Count) return int.MaxValue;//找不到
        List<int> res = new List<int>();
        for (int i = 0; i < a.Count; i++) {
            if (sortDic.ContainsKey(a[i])) {
                sortDic[a[i]] += 1;
            } else {
                sortDic.Add(a[i], 1);
            }
        }
        foreach (KeyValuePair<int, int> item in sortDic) {
            int temp = item.Value;
            while (temp-- != 0) {
                res.Add(item.Key);
            }
        }
        return res[res.Count - K];
    }
}

全部评论

相关推荐

10-29 19:45
吉林大学 Java
从零开始数:自我评价没有必要写,但是看起来你应该是学了csdiy的一些课程,可以在专业技能里面写上自己比较熟悉操作系统和计网,但如果你是找Java的话,把第一个项目换了吧,现在看起来有点四不像。 无论是黑马点评或者说做个轮子项目,刷题和八股也搞起来吧,而且也没必要等到寒假,最近就可以开始找,找到就偷偷实习呗,别被逮到就行了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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