[抄题]:

在一个排序矩阵中找从小到大的第 k 个整数。

排序矩阵的定义为:每一行递增,每一列也递增。

[思维问题]:

不知道应该怎么加,因为不是一维单调的。

[一句话思路]:

周围两个数给x或y挪一位, 如果hash数组没有就添加到minheap中

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. class Pair(没有参数)中要有数据类型、方法Pair(int x , int y, int val)
  2. PairComparator 继承了 Comparator<Pair>类 里面自然就是对pair进行比较, 接口<内容物>+名 = new 内部结构名<内容物>。Queue xx = new PQ<Integer>(k,新建pair对象) 0,0,matrix[0][0] 也要新建对象 new Pair
  3. x坐标用dx表示, 里面只有0,1
  4. 取出数从i = 1开始,不用从0开始。因为第一个符合条件的pair是(0,0,matrix[0][0])。
  5. 数组不包括,直接用!hash[x][y]即可

[二刷]:

  1. class关键字是小写开头
  2. next_pair新建之后,minheap直接add(next_pair)即可
  3. dx,dy数组的作用是加一位坐标,因此里面有东西:0 或 1

[三刷]:

  1. paircomparator类是继承来的,别的地方可以用,所以写class

[四刷]:

[五刷]:

[总结]:

[复杂度]:Time complexity: O(klgk) Space complexity: O(k)

[英文数据结构,为什么不用别的数据结构]:

heap符合每次取顶 丢掉,取到k个为止

[其他解法]:

二分法

[Follow Up]:

[LC给出的题目变变变]:

373. Find K Pairs with Smallest Sums 也是用minheap实现取最小

668. Kth Smallest Number in Multiplication Table 看着像用heap,其实用二分法 玩数学

719. Find K-th Smallest Pair Distance 其实用二分法 玩数学。看来二分法也能用于查找最小的第k个数

  1. public class Solution {
  2. /*
  3. * @param matrix: a matrix of integers
  4. * @param k: An integer
  5. * @return: the kth smallest number in the matrix
  6. */
  7. public int kthSmallest(int[][] matrix, int k) {
  8. class Pair {
  9. int x,y,val;
  10. public Pair(int x,int y,int val) {
  11. this.x = x;
  12. this.y = y;
  13. this.val = val;
  14. }
  15. };
  16. class PairComparator implements Comparator<Pair>{
  17. public int compare(Pair a,Pair b) {
  18. return a.val - b.val;
  19. }
  20. };
  21. int m = matrix.length;
  22. int n = matrix[0].length;
  23. int[] dx = new int[]{0,1};
  24. int[] dy = new int[]{1,0};
  25. boolean[][] hash = new boolean[m][n];
  26. Queue<Pair> minHeap = new PriorityQueue<Pair>(k,new PairComparator());
  27. minHeap.add(new Pair(0,0,matrix[0][0]));
  28. for (int i = 1; i < k; i++) {
  29. Pair cur = minHeap.poll();
  30. for (int j = 0; j < 2; j++) {
  31. int next_x = cur.x + dx[j];
  32. int next_y = cur.y + dy[j];
  33. Pair next_Pair = new Pair(next_x,next_y,0);
  34. if (next_x < m && next_y < n && !hash[next_x][next_y]) {
  35. next_Pair.val = matrix[next_x][next_y];
  36. hash[next_x][next_y] = true;
  37. minHeap.add(next_Pair);
  38. }
  39. }
  40. }
  41. return minHeap.peek().val;
  42. }
  43. }

排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix的更多相关文章

  1. lintcode-401-排序矩阵中的从小到大第k个数

    401-排序矩阵中的从小到大第k个数 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 样例 给出 k = 4 和一个排序矩阵: [ [1 ,5 ,7], [ ...

  2. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  3. 找到排序矩阵中从小到大第K个数字

    一 题目描述 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 二 题解 由于排序矩阵中的每一行都是递增的,并且每一列都是递增的.从小到大第k个数,实际上就是 ...

  4. python 判断矩阵中每行非零个数的方法

    python 判断矩阵中每行非零个数的方法: # -*- coding: utf-8 -*- # @Time : 2018/5/17 15:05 # @Author : Sizer # @Site : ...

  5. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  6. [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  7. LeetCode1337矩阵中最弱的K行

    题目 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...

  8. 找出一堆数中最小的前K个数

    描写叙述: 给定一个整数数组.让你从该数组中找出最小的K个数 思路: 最简洁粗暴的方法就是将该数组进行排序,然后取最前面的K个数就可以. 可是,本题要求的仅仅是求出最小的k个数就可以,用排序能够但显然 ...

  9. matlab求一个矩阵中各元素出现的个数(归一化)

    function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...

随机推荐

  1. 1117 Eddington Number (25 分)

    1117 Eddington Number (25 分) British astronomer Eddington liked to ride a bike. It is said that in o ...

  2. 如何下载spring的jar包?

    文转载至:https://www.cnblogs.com/yjmyzz/p/3847364.html Spring官网改版后,很多项目的完整zip包下载链接已经隐掉了,虽然Spring旨在引导大家用更 ...

  3. Spring mvc的web.xml配置详解

    1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...

  4. ue4 多相机分屏与小地图效果实现教程

    转自:http://blog.csdn.net/shenmifangke/article/details/51940007  通过使用ue4的UI和rendertarget来实现 优点就是可以随意设置 ...

  5. Ubuntu12.10下Vsftpd的安装

    安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...

  6. java的多态性(二)

    2013-10-16 19:44 9364人阅读 评论(25) 收藏 举报  分类: [JAVA开发]-----Java提高篇(36)  版权声明:本文为博主原创文章,未经博主允许不得转载.   目录 ...

  7. 存储设备的DDP功能详解

    http://blog.csdn.net/u013394982/article/details/18259015 DDP功能,即Dynamic Disk Pool,它是除了现有的RAID0,1,10, ...

  8. 常用模块:os模块,logging模块等

    一    os模块 那么作为一个常用模块,os模块是与操作系统交互的一个模块. 那么os模块中我们常用的一般有以下几种: os.listdir('dirname') 以列表的形式列出指定目录下的所有文 ...

  9. js运动框架逐渐递进版

    运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓冲运动. 多物体运动 ...

  10. .NET MVC 两种视图引擎(Razor、Aspx)

    ASPX 优点:         通过上面小小的对比,不难看出,与ASP.NET MVC紧密集成,对于以往ASP.NET开发人员有更好体验.其实它还有其他几优点:         ●智能感应      ...