题解 | #寻找第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];
    }
}

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
秋招吐槽大会
点赞 评论 收藏
分享
牛马人的牛马人生:500一天吗?香麻了
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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