题目:

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. ASP.NET MVC搭建项目后台UI框架—4、tab多页签支持

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  2. java web学习总结(十六) -------------------数据库连接池

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  3. 【翻译】jQuery是有害的

    原文:http://lea.verou.me/2015/04/jquery-considered-harmful/**(第一次翻译,望大家多批评指正) jQuery是有害的 嗨,我总想写一个“X”是有 ...

  4. iOS 大文件断点下载

    iOS 在下载大文件的时候,可能会因为网络或者人为等原因,使得下载中断,那么如何能够进行断点下载呢? // resumeData的文件路径 #define XMGResumeDataFile [[NS ...

  5. SU54 新建视图簇 维护数据表

    由于一些数据表的数据比较重要,不允许直接查看数据表中的数据或者通过SM30进行维护, 故可以通过新建一个视图簇的方式来实现,然后通过程序调用这个视图簇,来进行数据的维护. 运行事务码SE54 最后通过 ...

  6. Java中 实现多线程成的三种方式(继承,实现,匿名内部类)

    ---------------------------------------------------------------------------------------------------- ...

  7. 活用UML-软件设计高手(深圳 2014年4月26-27日)

      我们将在深圳为您奉献高级技术课程”活用UML-软件设计高手“,首席专家张老师将会为您分享软件架构设计.数据库设计.用户体验设计及详细设计的最佳实践,帮助您成为优秀的软件设计师! 时间:2014.0 ...

  8. Mindjet MindManager思维导图工具的使用 - 项目管理系列文章

    在项目管理过程中,不可避免的要使用到各种各样的项目管理工具.本文就对我自己使用的Mindjet Manager进行一下介绍,推荐给大家使用. 1.  打开软件 这里使用了2012版 2.  软件的界面 ...

  9. Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

    使用Microsoft SQL SERVER 2014 Management Studio访问Azure SQL Database时,查看存储过程时遇到下面错误信息: TITLE: Microsoft ...

  10. easyui窗口组件

    注意首先要在title后面导入配置文件,前后顺序不能乱 <!-- 1.JQuery的js包 --><script type="text/javascript" s ...