题目:

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. MongoDB学习笔记—Linux下搭建MongoDB环境

    1.MongoDB简单说明 a MongoDB是由C++语言编写的一个基于分布式文件存储的开源数据库系统,它的目的在于为WEB应用提供可扩展的高性能数据存储解决方案. b MongoDB是一个介于关系 ...

  2. 让网站动起来!12款优秀的 jQuery 动画插件推荐

    如今,大多数设计师和开发人员被要客户要求开发动态的网站.创造视觉震撼和醒目的动态网站是艰巨的任务,因为它需要大量的努力和创造力.在网络上有大量的工具和插件可用于创建网站动画.许多开发人员正在使用 HT ...

  3. iOS NSArray数组过滤

    需求:在一个数组里面,将在这个数组中的并且在另一个数组里面的元素过滤掉. 即:在一个数组dataArray里面,将在dataArray数组中的并且在filteredArray数组里面的元素过滤掉. / ...

  4. .NET破解之百度云盘分享工具(批量)

    似曾相识 百度云盘分享工具一款专门用于自动批量分享百度云文件的软件.其原理完全模拟在网页上登录百度云盘,模拟手工点击,将分享的"公共链接"或"私密链接"保存起来 ...

  5. Linux安全基础:vi的使用

    1.vi的三种模式(1)一般模式(2)编辑模式(3)指令模式 2.模式切换键入i/o/a进入编辑模式键入:/,或/进入指令模式按esc退回一般模式保存wq强制保存wq!退出q强制退出q! 3.一般模式 ...

  6. 关于SharePoint 2013的工作流(一)

    从去年开始,一直和SharePoint 2013工作流打交道.自己瞎摸索,以实现功能为目的.直到如今也不知道走的路是否正确. 一开始用WF4发现整个都不一样了,用的xaml无法写后端代码.Google ...

  7. Java的异常处理

    Java的异常处理是通过5个关键字来实现的:try,catch,throw,throws,finally.JB的在线帮助中对这几个关键字是这样解释的:       Throws: Lists the ...

  8. 国外干货!6个方法助你设计出优秀的APP

    伟大的设计来源于一致性和细致化,而其实只要有足够的纪律,每个团队都可以实现这一点. 品牌(源码:http://www.jinhusns.com/Products/Download/?type=xcj) ...

  9. 利用split

    java.lang.string.splitsplit 方法将一个字符串分割为子字符串,然后将结果作为字符串数组返回.stringObj.split([separator,[limit]])strin ...

  10. .NET开源插件内核

    http://www.cnblogs.com/newmin/ .NET开源插件内核:支持WinForm和Asp.net. 设计的初衷是:利用“开发平台 + 插件内核"来开发子系统,及对系统进 ...