版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址

http://blog.csdn.net/lzuacm

C#版 - Leetcode 215. Kth Largest Element in an Array-题解

在线提交: https://leetcode.com/problems/kth-largest-element-in-an-array/

Description


Find the k th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note:

You may assume k is always valid, 1 ≤ k ≤ array’s length.



思路:

使用List,定义其Sort函数接口,取出第k大的值(List的第k-1个元素)即可。

已AC代码:

public class Solution
{
    public int FindKthLargest(int[] nums, int k)
    {
        int kthMax = 0;
        var list = new List<int>();
        foreach (var num in nums)
            list.Add(num);
        list.Sort((x, y) => -x.CompareTo(y));

        if (list.Count >= k)
            kthMax = list.ElementAtOrDefault(k-1);

        return kthMax;
    }
}

Rank:

You are here!

Your runtime beats 94.55 % of csharp submissions.

C#版 - Leetcode 215. Kth Largest Element in an Array-题解的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  4. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  6. Java for LeetCode 215 Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

  9. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

随机推荐

  1. linux 端口占用

    进程id为9106,进程名称为java的进程,占用了8080端口(监听了8080端口)

  2. 深圳同城快跑笔试题目 2 实现json字符串保存到本地硬盘

    //从给定位置读取Json文件 public static String readJson(String path){ //从给定位置获取文件 File file = new File(path); ...

  3. 20181125第二章节总结part3

    数据-元祖 元祖的是可存放多个值,不可变,有顺序的,从左向右编号. 作用是可以用来存储一些不可以更改的配置文件 基本 语法: #创建新元祖 tuple = (,,,,,) #索引,写法同list tu ...

  4. Unity-修改Debug日志文本颜色

    Unity开发过程中Debug信息是非常重要的,但是千篇一律的白色字符不能迅速找出想要的信息,添加些字体颜色是个很好的办法,比如: AppDebug.Log("<color=#ff84 ...

  5. LINUX监控-spotlight

    这里连接的user不能是root(spotlight需要一个具有root权限的用户,但是又不允许是root),所以需要给要连接的linux端创建一个有root权限的用户,在linux主机创建了root ...

  6. 181102 Python环境搭建(安装Sublime Text3)

    利用Pycharm来编写.执行python代码是一个不错的选择,Pycharm的安装的确也很方便.但是偶然看到别人用Sublime Text来编写.执行代码,觉得很酷.所以自己动手搭建环境. 1. 下 ...

  7. 【原创】XAF ITreeNode+NonPersistent 使用方式

    在XAF中使用非持久化对象创建出TreeList这种树形结构 private void SetShowRFID(TArchivesBorrow archivesInStorage, string rf ...

  8. 0~5年一个Java程序员的晋升之路

    在程序界流行着一种默认的说法叫“黄金5年”,也就是一个程序员从入职的时候算起,前五年的选择直接影响着整个职业生涯中的职业发展方向和薪资走向,如何走好这5年,彻底从一个刚入行的菜鸟蜕变成可以以不变应万变 ...

  9. C语言复习3_条件结构

    if条件结构 if else 结构一般处理区间情况 #include <stdio.h> #include <stdlib.h> int main() { //打印剧情 dou ...

  10. Python练手例子(10)

    55.学习使用按位取反~. 程序分析:~0=1; ~1=0; (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) (3)将上面二者进行&运算. ...