public class Solution {
public int KthSmallest(int[,] matrix, int k) {
var row = matrix.GetLength();
var col = matrix.GetLength(); var list = new List<int>(); for (int i = ; i < row; i++)
{
for (int j = ; j < col; j++)
{
list.Add(matrix[i, j]);
}
} list = list.OrderBy(x => x).ToList();
return list[k - ];
}
}

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/description

补充一个多次使用二分查找的解决方案:

 public class Solution {
public int KthSmallest(int[][] matrix, int k) {
int low = matrix[][];
int high = matrix[matrix.Length - ][matrix[].Length - ];
int count = ;
while (low <= high)
{
int mid = low + (high - low) / ;
count = GetCount(matrix, mid);
if (count < k)
{
low = mid + ;
}
else
{
high = mid - ;
}
}
return low;
} private int GetCount(int[][] matrix, int num)
{
int ans = ;
int i = ;
int j = matrix[].Length - ;
while (i < matrix.Length && j >= )
{
if (matrix[i][j] > num)
{
j--;
}
else
{
ans += j + ;
i++;
}
}
return ans;
}
}

leetcode378的更多相关文章

  1. [Swift]LeetCode378. 有序矩阵中第K小的元素 | Kth Smallest Element in a Sorted Matrix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  2. leetcode378 Kth Smallest Element in a Sorted Matrix

    思路1: 使用堆. 实现: class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...

  3. leetcode378 有序矩阵中第k小的元素

    排序后取数组第k个元素,遍历需要n^2的复杂度,查找插入logn,时间复杂度O(n^2logn).方法很笨,完全就是STL过于牛x运行通过的. class Solution { public: int ...

  4. leetcode探索高级算法

    C++版 数组和字符串 正文 链表: 正文 树与图: 树: leetcode236. 二叉树的最近公共祖先 递归(先序) leetcode124二叉树最大路径和 递归 图: leetcode 547朋 ...

  5. 牛客网在线编程_有序矩阵中第K小的元素

    Leetcode378原题,所以一样没有数据范围...( log(max-min)二分答案,然后NlogN二分每一行求出小于答案的元素个数,为了保证二分的答案在矩阵中,二分写的要和平常不太一样,最后输 ...

随机推荐

  1. 【枚举】【最小生成树】【kruscal】bzoj3754 Tree之最小方差树

    发现,若使方差最小,则使Σ(wi-平均数)2最小即可. 因为权值的范围很小,所以我们可以枚举这个平均数,每次把边权赋成(wi-平均数)2,做kruscal. 但是,我们怎么知道枚举出来的平均数是不是恰 ...

  2. 019对象——对象 method_exists property_exists instanceof

    <?php /** * 19 对象 method_exists property_exists instanceof */ //method_exists() 判断方法是否存在,第一个参数对象或 ...

  3. CSS:Tutorial two

    1.CSS Text text color, text align... Text Decoration The text-decoration property is used to set or ...

  4. redis 内存库设置 教你怎么解决64位Windows版Redis狂占C盘的问题.

    http://blog.csdn.net/renfufei/article/details/41180007 # heapdir指定内存映射文件路径名,不能是文件名 # heapdir <dir ...

  5. 《Java程序员职场全攻略 从小工到专家》 - 书摘精要

    (前言) 学习招式在次,提升内力才是最主要的: (P10) 选择一门编程语言,只是入门的途径.过分依赖编程语言,只会让自己成为代码高手,而不是开发大牛,要知道编程语言只是一种工具,更重要的是编程思想: ...

  6. pip国内镜像(清华大学镜像)

    网上搜到的pip国内镜像大部分是豆瓣的 http://pypi.douban.com/simple/ 但是根本不全,很多包没有 所以推荐清华大学的 https://pypi.tuna.tsinghua ...

  7. 数据库查询操作(fetchone,fetchall)

    数据库查询操作 Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据. fetchone(): 该方法获取下一个查询结果集.结果集是一个 ...

  8. 【操作系统】总结五(I/O管理)

    输入输出管理本章主要内容: I/O管理概述(I/O控制方式.I/O软件层次结构)和I/O核心子系统(I/O调度概念.局速缓存与缓冲区.设备分配与回收.假脱机技术(SPOOLing)). 5.1 I/O ...

  9. js客户端UI框架

    Best jQuery UI http://b-jui.com/ jQuery EasyUI http://www.jeasyui.com/ bootstrap学习网: http://www.runo ...

  10. 笔记:开源协议 Apache 2 和 GPL 兼容

    笔记:开源协议 Apache 2 和 GPL 兼容 Apache 2 和 GPL v3 兼容. GPL 分了很多版本,LGPL 为最宽松的 GPL,而 AGPL 为最严格的 GPL 协议. Linux ...