题目:

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

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8, return 13.
  给了一个n×n的矩阵,每行、每列都是递增的,要我们找出最小的第k个值。注意:允许重复数字。(本来还想用set的这下不行了。。。老老实实用了list) 方法:
  没用高级算法,胜在思路简单。。。先思考写出了一个可行版本。
方法关键在于:
  1. 单独设立一个list,每次从里面筛选最小值出栈。(简单的把它称为“栈”)对于第一列,但不是最后一行的元素出栈,则将它右边和下面的元素进栈。
  2. 第一列但是最后一行的元素出栈,则将它右边元素进栈。
  3. 对于非第一列元素,如果又不是最后一列,则将它右边元素进栈。对于非第一列但是最后一列的元素出栈,则什么都不做(即每行最后一个元素)。
  4. 00位置一定是第一个最小值,那么对于k>1的情况初始化01,10位置进list开始在list里面筛选最小值。调回第一步。

注意:矩阵只有1个元素,和k=1两种特殊情况。

代码:

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
typedef struct node
{
int row;
int lie;
int data;
}node;
node popMin(list<node> & compare){
auto iter=compare.cbegin();
node min;
min=(*iter);
while (iter!=compare.cend()) {
if (min.data>(*iter).data) {
min=(*iter);
}
iter++;
}
//当前最小值出栈
iter=compare.cbegin();
while (iter!=compare.cend()) {
if ((*iter).data==min.data) {
compare.erase(iter);
break;
}
iter++;
}
//返回最小值,信息包括:最小值data,最小值位于行、列
return min;
}
int main(int argc, const char * argv[]) {
vector<vector<int>> matrix={{1,2},{1,3}};
//矩阵的行列值
auto matrixRow=matrix.size();
auto matrixLie=matrix[0].size();
list<node>compare;
int k=1;
int count=1;
node min;
int ans=0;//返回第k小元素
if (matrixRow>1) {
//初始化compare双向链表
node tempnode;
tempnode.data=matrix[0][1];
tempnode.row=0;
tempnode.lie=1;
compare.push_back(tempnode);
tempnode.data=matrix[1][0];
tempnode.row=1;
tempnode.lie=0;
compare.push_back(tempnode);
if(1<k){
while (count<k) {
min=popMin(compare);
count++;//每找到一个最小值,count+1
//第一列
if(min.lie==0){
if (min.row<matrixRow-1) {//非最后一行
//下边入栈
tempnode.data=matrix[min.row+1][min.lie];
// cout<<tempnode.data<<endl;
tempnode.row=min.row+1;
tempnode.lie=min.lie;
compare.push_back(tempnode);
//右边入栈
tempnode.data=matrix[min.row][min.lie+1];
// cout<<tempnode.data<<endl;
tempnode.row=min.row;
tempnode.lie=min.lie+1;
compare.push_back(tempnode);
}else if(min.row==matrixRow-1){//最后一行
//右边入栈
tempnode.data=matrix[min.row][min.lie+1];
// cout<<tempnode.data<<endl;
tempnode.row=min.row;
tempnode.lie=min.lie+1;
compare.push_back(tempnode);
}
}else{//非第一列
if (min.lie<matrixLie-1) {//非最后一列
//右边入栈
tempnode.data=matrix[min.row][min.lie+1];
// cout<<tempnode.data<<endl;
tempnode.row=min.row;
tempnode.lie=min.lie+1;
compare.push_back(tempnode);
}else if (min.lie==matrixLie-1){//一列
//没有可以入栈的
continue;
}
}
}
ans=min.data; }else if(k==1){
ans=matrix[0][0];
}
}else{
ans=matrix[0][0];
}
cout<<ans<<endl;
return 0;
}

  

Leetcode:378. Kth Smallest Element in a Sorted Matrix的更多相关文章

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

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

  2. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. 【Leetcode】378. Kth Smallest Element in a Sorted Matrix

    Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...

  4. 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)

    Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...

  5. 378. 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 ...

  6. 378. 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 ...

  7. 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)

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

  8. 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [ ...

  9. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

随机推荐

  1. 深入理解和应用display属性(二)

    四.inline-block 此类元素是inline + block的合体 1) margin和padding都有效:width和height都有效: .inline{ display: inline ...

  2. SVG图案填充-Pattern

    SVG图案一般用于SVG图形对象的填充fill或描边stroke.这个图形可以是一个SVG元素,也可以是位图图像,通过<pattern>元素在x轴或y轴方向以固定的间隔平铺. <pa ...

  3. window对象的属性及事件。

    不同的运行环境有不同的“顶层对象”,而在浏览器的环境中,顶层对象就是window对象.window就是指当前的浏览器窗口. 例:var a = 1: window.a; //1 1.window对象的 ...

  4. VS中如何快捷地给自己的代码添加创建信息注释

    VS中如何快捷地给自己的代码添加创建信息注释 Intro 以下讨论的都是没有使用 GIT 来管理源代码的情况,如果使用 GIT 管理源代码可直接使用VS的Git扩展就不需要考虑以下问题. 什么是创建信 ...

  5. 使用原生JS实现一个风箱式的demo,并封装了一个运动框架

    声明,该DEMO依托于某个培训机构中,非常感谢这个培训结构.话不多说,现在开始改demo的制作. 首先,在前端的学习过程中,轮播图是我们一定要学习的,所以为了更加高效的实现各种轮播图,封装了一个运动的 ...

  6. Aircrack-ng: (1) 概述

    目录 一.概述 二.工具与命令介绍 Linux命令 (1) ifconfig (2) macchanger (3) iwconfig (4) iwlist Aircrack-ng 工具 (1) air ...

  7. 关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?

    1.概述: SharePoint 2013(包括SharePoint 2010)提供了很方便的,多选的界面,但是很多操作还是不能批量进行,比如:批准的功能.如果您要解决方案不关心代码,那么请直接联系作 ...

  8. shiro 实现单用户登录,一个用户同一时刻只能在一个地方登录

    我这里 shiro 并没有集成 springMVC,直接使用 ini 配置文件. shiro.ini [main] # Objects and their properties are defined ...

  9. tabbarItem字体及图片颜色设置

    tabbarItem设置图片后运行往往与我们原始图片颜色有出入,这是因为在默认情况下,未选中状态图片和字体颜色为灰色,选中状态下图片和字体颜色为蓝色.   UIImage 在呈现(render)时会选 ...

  10. TOP命令各个参数代表意义详解

    Top命令是Linux下常用的系统性能分析工具,能实时查看系统中各个进程资源占用情况. top - 16:24:25 up 284 days, 4:59, 1 user, load average: ...