leetcode766
本题经过一下午的思考,终于解出来了。使用的是层次遍历的思想。
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int RowLen = matrix.size() - ;
int ColLen = matrix[].size() - ;
int N = RowLen + ColLen;
int i = RowLen;
int j = ;
queue<pair<int, int>> Q;
Q.push(make_pair(i, j));
while (!Q.empty())
{
//全部出队,加入vector
vector<pair<int, int>> V;
while (!Q.empty())
{
pair<int, int> p = Q.front();
Q.pop();
V.push_back(p);
cout << p.first << " " << p.second << endl;
}
cout << "------------------------------------------" << endl;
//遍历V
int base = matrix[V[].first][V[].second];
set<pair<int, int>> S;
for (auto v : V)
{
//判断是否都是相同的数值
if (base != matrix[v.first][v.second])
{
return false;
}
//判断“上”和“右”的元素是否合法,
int Up_x = v.first - ;
int Up_y = v.second;
//“上元素”合法则加入S(去重)
if (Up_x >= && S.find(make_pair(Up_x, Up_y)) == S.end())
{
S.insert(make_pair(Up_x, Up_y));
}
int Right_x = v.first;
int Right_y = v.second + ;
//“右元素”合法则加入S(去重)
if (Right_y <= ColLen && S.find(make_pair(Right_x, Right_y)) == S.end())
{
S.insert(make_pair(Right_x, Right_y));
}
}
//将S中的元素,添加到Q中
for (auto s : S)
{
Q.push(s);
}
}
return true;
}
};
leetcode766的更多相关文章
- [Swift]LeetCode766. 托普利茨矩阵 | Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- 算法22-----托普利茨矩阵leetcode766
1.题目 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵. 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: matr ...
- Leetcode766.Toeplitz Matrix托普利茨矩阵
如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵. 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: matrix = ...
随机推荐
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java基础学习-IO流
package IObasics; import java.io.FileWriter; import java.io.IOException; /*IO流 * 通过数据流.序列化和文件系统提供系统输 ...
- PostgreSQL日志配置记录
日志审计 审计是值记录用户的登陆退出以及登陆后在数据库里的行为操作,可以根据安全等级不一样设置不一样级别的审计, 此处涉及的参数文件有: logging_collector --是否开启日 ...
- python学习网址
http://kuanghy.github.io/categories/#Python
- MySQL 5.7.18 在centos下安装记录
一个朋友找我如何在linux下安装mysql5.7.18,我稍微整理下了下记录,如下: 下载地址: MySQL5.7.18参数官方网址:https://dev.mysql.com/doc/refman ...
- How do I create zip file in Servlet for download?
原文链接:https://kodejava.org/how-do-i-create-zip-file-in-servlet-for-download/ The example below is a s ...
- laravel 框架给数组分页
//Get current page form url e.g. &page=6 $currentPage = LengthAwarePaginator::resolveCurr ...
- 微软白板Excel xls列号数字转字母
Excel xls列号数字转字母 https://blog.csdn.net/lf124/article/details/53432817?utm_source=itdadao&utm_med ...
- WCF OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout
1.OpenTimeout 客户端与服务端建立连接时,如果超过指定时间都还没完成,就引发TimeoutException. 在TCP通讯中,服务器必须首先准备好侦听端口并在该端口上侦听(Listen) ...
- 记录几个基础的SQL开发题
1. 表A有5行数据,表B有7行数据,问Inner Join最多返回几行数据,Left Join最多返回几行数据,分别在什么情况下? Inner Join 是返回关联表的Cartesian produ ...