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 = ...
随机推荐
- 【lightoj-1025】The Specials Menu(区间DP)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 [题目大意] 求一个字符串删去任意字符可以构成多少个不同的回文串 [分析 ...
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- react:高阶组件wrappedComponent
什么是高阶组件? 高阶部件是一种用于复用组件逻辑的高级技术,它并不是 React API的一部分,而是从React 演化而来的一种模式. 具体地说,高阶组件就是一个接收一个组件并返回另外一个新组件的函 ...
- Django -- DRF 认证流程
Django Restful Framework (DRF)中类的调用与自定义-- 以 autentication 认证为例 DRF 的 request 对 django 的 request 进行了更 ...
- File I/O的总结
1读写字符文件 BufferedReader br=new BufferedReader(new FileReader("文件路径")); BufferedWriter bw=ne ...
- SQL-left(right,inner) join
left join(左联接) :返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) :返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) ...
- 防止前端脚本JavaScript注入
在使用ajax进行留言的时候,出现了一个问题.因为留言内容写完之后,通过ajax提交内容,同时使用js把留言的内容添加到页面上来.浏览留言的时候也是通过ajax请求,然后再显示的.这样,如果有人在留言 ...
- GitLab non-standard SSH port
/***************************************************************************** * GitLab non-standard ...
- python学习之多线程(一)
引入线程包或者命名空间import threading 一:建立一个简单的线程程序 import time, threading def test(): print('thread %s is ...
- freemarket使用自定义标签 注解【项目实际使用】
页面达到效果 [主html页面代码] <!-- 合作单位 版块 -->[#include 'inc_project_succ_coo.html'/]['inc_project_succ_c ...