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. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem

    D. Dasha and Very Difficult Problem time limit per test:2 seconds memory limit per test:256 megabyte ...

  2. CSS:Tutorial four

    1.CSS Tables To specify table borders in CSS, use the border property. The example below specifies a ...

  3. DRF 的 版本,解析器,与序列化

    DRF 的 版本,解析器,与序列化 补充 配置文件中的 类的调用: (字符串) v1 = ["view.xx.apth.Role","view.xx.apth.Role& ...

  4. Android中Activity的LauchMode(加载模式)

    1.standard模式:一个task有多个Activity,一个Activity可以被实例化多次,可以放在不同的task中. 2.singleTop模式:该Activity在栈顶,同时收到启动该Ac ...

  5. 在IIS站点中使用数字证书

    1. SSL解析(内容来自百度百科) SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安 ...

  6. CSS命名规范参考及书写注意事项

    CSS书写顺序 *{ /*显示属性*/ display position float clear cursor … /*盒模型*/ margin padding width height /*排版*/ ...

  7. Ubantu下安装FTP服务器

    在Linux中ftp服务器的全名叫 vsftpd,我们需要利用相关命令来开启安装ftp服务器,然后再在vsftpd.conf中进行相关配置,下面我来介绍在Ubuntu中vsftpd安装与配置增加用户的 ...

  8. maven依赖顺序原则

    使用maven的程序员都会遇到一个问题,那就是maven依赖冲突的问题,这会导致ClassNotFound或者MethodNotFound这样的异常.其实只要明白maven依赖的根本性的原则就不怕这样 ...

  9. MTK驱动移植相关路径

    转自:http://blog.csdn.net/yicao821/article/details/52314578 一.Flash兼容 bootable/bootloader/preloader/to ...

  10. 常用JavaScript操作页面元素的方法

    1.取得dropdownlist的选中值 var ddl =document.getElementById('<%=ddlusers.ClientID%>'); var index = d ...