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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- CSS3关于background-size属性
bachground-size属性就是定义背景图片的大小,其值有:auto , 像素值 , 百分比 , cover , contain . background-size: auto,默认值,以图 ...
- AMD and CMD are dead之KMDjs集成Blob一键下载全部build包
更新 不zuo,[A/C]MD就不会死,所以kmdjs赢来来其伟大的版本0.0.6,该版本主要的更新有: 移除去了kmdjs.get(..).then的支持,只支持kmdjs.get(-,functi ...
- Aircrack-ng: (1) 概述
目录 一.概述 二.工具与命令介绍 Linux命令 (1) ifconfig (2) macchanger (3) iwconfig (4) iwlist Aircrack-ng 工具 (1) air ...
- Civil 3D API二次开发学习指南
Civil 3D构建于AutoCAD 和 Map 3D之上,在学习Civil 3D API二次开发之前,您至少需要了解AutoCAD API的二次开发,你可以参考AutoCAD .NET API二次开 ...
- Cordova中使用gulp
打开package.json,添加main:gulpfile.js 在dependencies中添加gulp,vs2015十分智能,可以智能从npm中获取依赖如下图: 在添加过程中注意 ...
- IOS开发基础知识--碎片33
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
- iOS 疑难杂症 — — 在 Storyboard 里 Add Size Class Customization 后再从代码里无法修改的问题
前言 公司的产品同时适配 iPhone 和 iPad ,并坚持用 Storyboard 来做适配,今天又踩一个坑(以前遇到过)还以为是 XCode 的鬼毛病. 声明 欢迎转载,但请保留文章原始出处: ...
- 【产品 & 设计】入门 - 工具篇 - Sketch + Skala Preview
前言 做产品和设计快 1 年了,积累了一点经验分享一下 —— 抛砖引玉,欢迎交流. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: ht ...
- android 音乐播放器
本章以音乐播放器为载体,介绍android开发中,通知模式Notification应用.主要涉及知识点Notification,seekbar,service. 1.功能需求 完善音乐播放器 有播放列 ...
- Android性能优化之内存优化练习
练习题目地址:https://github.com/lzyzsd/MemoryBugs 分析及优化过程如下: 问题1 静态变量引用activity 使用神器LeakCanary检查内存泄露问题 从图中 ...