Matrix Cells in Distance Order
Matrix Cells in Distance Order
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
Additionally, we are given a cell in that matrix with coordinates (r0, c0).
Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)
Example 1:
Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]
Example 2:
Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
Example 3:
Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
Note:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C
Code
//
// main.cpp
// 最短最长距离
//
// Created by mac on 2019/7/21.
// Copyright © 2019 mac. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <unordered_map> //Hash Table
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0)
{
vector<vector<int>> res;
vector<vector<vector<int>>> box(R+C-1);
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
{
vector<int> temp={i,j};
int index=abs(i-r0)+abs(j-c0);
box[index].push_back(temp);
}
for(auto i:box)
if(i.size())
for(auto j:i) res.push_back(j);
return res;
}
};
int main(int argc, const char * argv[]) {
int R=2;
int C=2;
int r0=0;
int c0=1;
Solution so;
vector<vector<int>> test = so.allCellsDistOrder(2, 2, 0, 1);
cout<<"[";
for (auto i : test) {
cout<<"[";
for (auto j :i) {
cout<<j<<",";
}
cout<<"],";
}
cout<<"]";
// vector<int> a(3);
// a={1,2,3,4};
// vector<vector<int>> b,e;
// vector<vector<vector<int>>> c;
//
// b.push_back(a);
// b.push_back({2,3,4,5,9});
// b.push_back({1,2,3,89,44});
//
//
// e.push_back({9,3,4,5,9});
// e.push_back({8,2,3,89,44});
//
// c.push_back(b);
// c.push_back(e);
//
// for(auto i:c){
// for (auto j:i) {
// for (auto k : j) {
// cout<<k<<" ";
// }
// cout<<endl;
// }
// cout<<endl<<endl;
// }
//=============================
// for (int k=0; k<b.size(); ++k) {
// for (int i=0; i<a.size(); ++i) {
// cout<<b[k][i]<<" ";
// }
// cout<<endl;
// }
return 0;
}
运行结果
[[0,1,],[0,0,],[1,1,],[1,0,],]Program ended with exit code: 0
参考文献
Matrix Cells in Distance Order的更多相关文章
- 【Leetcode_easy】1030. Matrix Cells in Distance Order
problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...
- [Swift]LeetCode1030. 距离顺序排列矩阵单元格 | Matrix Cells in Distance Order
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 & ...
- 【leetcode】1030. Matrix Cells in Distance Order
题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...
- 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- LeetCode.1030-曼哈顿距离排序矩阵单元格(Matrix Cells in Distance Order)
这是小川的第384次更新,第412篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第246题(顺位题号是1030).我们给出一个矩阵,其中R行和C列具有整数坐标(r,c)的 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Leetcode 第133场周赛解题报告
今天参加了leetcode的周赛,算法比赛,要求速度比较快.有思路就立马启动,不会纠结是否有更好的方法或代码可读性.只要在算法复杂度数量级内,基本上是怎么实现快速就怎么来了. 比赛时先看的第二题,一看 ...
- Weekly Contest 133
1030. Matrix Cells in Distance Order We are given a matrix with R rows and C columns has cells with ...
- 算法与数据结构基础 - 排序(Sort)
排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...
随机推荐
- 使用vault pki 为nginx 生成tls 证书文件
关于vault pki 管理的使用的可以参考官方文档或者docker-vault 以下演示一个简单的基于vault pki 为nginx 提供tls 证书 项目环境配置 nginx 配置文件 wo ...
- globing通配符
匹配标点符号 linux中只要不含有/的文件就可以生成,所以标点符号也是符合要求的 匹配空白 使用\对空白进行转义,这样就可以生成包含空格名称的文件 但是不推荐这样用,容易让别人在使用的时候造成误解 ...
- PHP生成随机数;订单号唯一
//8-12位随机数 function makeRand($num=){ $strand = (; if(strlen($strand)<$num){ $strand = str_pad($st ...
- Processing设计Android APP(1) - 安装
1.安装环境: A. Android Studio B. Processing 3.4 (64bit) 首先,直接安装Android Studio,我这里版本是3.2.1. 然后,新建一个Sample ...
- 在Matlab中画图输出
在Matlab中画图后,可能会调整格式.输出存储时,格式会忽然消失. 可以修改右下边Export setup,将Font size设置成auto. 这样就保留了编辑效果.
- 卷积神经网络CNN学习笔记
CNN的基本结构包括两层: 特征提取层:每个神经元的输入与前一层的局部接受域相连,并提取该局部的特征.一旦该局部特征被提取后,它与其它特征间的位置关系也随之确定下来: 特征映射层:网络的每个计算层由多 ...
- Linux系统学习(一)一Linux介绍
一.Linux初识 1.1 Linux是什么 Linux是一种自由和开放源码的类UNIX操作系统,使用Linux内核.目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机.平板 ...
- 团队作业-Beta冲刺(1/4)
队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...
- mocha单元测试简易教程
mocha单元测试简易教程 写在前面 其实mocha单元测试的教程网上有很多,也都很简单易懂,但是每个人对同一份的教程也会产生不同的理解,像我这种大概就是走遍了所有弯路才到达终点的人,想通过给大家分享 ...
- enq: DX – contention等待事件解决方法
前几日,一测试环境在dblink单表同步的时候(不管怎么说,目前仍然是同构数据库同步性能最快的方法,别听网上的扯淡,无论goldengate还是java层,都是比较慢的),某张表一直同步不过去,看了一 ...