Leetcode: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 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) 方法:
没用高级算法,胜在思路简单。。。先思考写出了一个可行版本。
方法关键在于:
- 单独设立一个list,每次从里面筛选最小值出栈。(简单的把它称为“栈”)对于第一列,但不是最后一行的元素出栈,则将它右边和下面的元素进栈。
- 第一列但是最后一行的元素出栈,则将它右边元素进栈。
- 对于非第一列元素,如果又不是最后一列,则将它右边元素进栈。对于非第一列但是最后一列的元素出栈,则什么都不做(即每行最后一个元素)。
- 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的更多相关文章
- [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 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- tomcat 自定义classpath(亲自测试)
因为一直以来使用tomcat和weblogic作为应用服务器为主,最近在升级新中间件的过程中遇到一个问题,我们的web前端应用现在升级是进行全量包升级的,因为现在的系统架构为前端和后端通过rpc框架交 ...
- 【转】【译】JavaScript魔法揭秘--探索当前流行框架中部分功能的处理机制
推荐语: 今天推荐一篇华为同事的同事翻译的一篇文章,推荐的主要原因是作为一个华为员工居然晚上还能写文章,由不得小钗不佩服!!! 其中的jQuery.angular.react皆是十分优秀的框架,各有特 ...
- Bootstrap之栅格系统
bootstrap 移动优先 中文官网 http://www.bootcss.com/ 1.基本模板 <!DOCTYPE html> <html lang="en&quo ...
- 各大互联网公司前端面试题(HTML/CSS)
Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...
- IOS开发基础知识--碎片39
1:UIWindow知识点 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...
- iOS之触摸及手势
触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了 ...
- SE(homework2)_软件分析
老师这次课后的作业具有开放性,很容易的我会想到经常用的那些工具软件,MATLAB,envi,ARCGIS等等. Q1:此类软件是什么时候出现的,这些软件是怎么说服你(陌生人)成为它们的用户的?他们的目 ...
- ToList()方法
//ToList()方法,翻译:把****转化为List集合. // 控制台试试: string[] fruits = { "apple", "passionfruit& ...
- AjaxHelper简介
在Asp.Net MVC中提供了AjaxHelper类: Ajax.ActionLink 创建一个超链接,点击时,使用Ajax提交数据到一个指定的控制器 Ajax.RouteLink 和ActionL ...
- Redhat Server 5.7 安装配置PHP
PHP的简介 PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf 为了要维护个人网页而制作的一个简单的用Perl语言编写的程序.这些工具程序用来显示 Rasm ...