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).希尔 ...
随机推荐
- 58、Spark Streaming: DStream的output操作以及foreachRDD详解
一.output操作 1.output操作 DStream中的所有计算,都是由output操作触发的,比如print().如果没有任何output操作,那么,压根儿就不会执行定义的计算逻辑. 此外,即 ...
- sublime text 3插件改造之添加从模版新增文件到指定目录
简介:以前使用ST2里面的Sublime NFFT插件比较顺手,最近安装了ST3,但是Sublime NFFT插件不支持ST3,就下载了SublimeTmpl从模版新建文件插件.在使用时,习惯在侧边栏 ...
- join 分割数组
返回一个字符串.该字符串是通过把 arrayObject 的每个元素转换为字符串,然后把这些字符串连接起来,在两个元素之间插入 separator 字符串而生成的. separator可以传可以传,不 ...
- avalon用background-image不起作用,怎么来选取前几个的图片进行渲染
<span ms-css="{backgroundImage: 'url('+item.image + ')'}" ms-for="($index,item) in ...
- fluent加载第三方(C++,Fortan等)动态链接库
这里我介绍一种比较简单的方法,首先我们从ANSYS Fluent UDF Manual上随便找一段正确的UDF,下面这段UDF取自ANSYS 18的ANSYS Fluent UDF Manual,位于 ...
- 刷题记录:[CISCN2019 总决赛 Day2 Web1]Easyweb
目录 刷题记录:[CISCN2019 总决赛 Day2 Web1]Easyweb 一.涉及知识点 1.敏感文件泄露 2.绕过及sql注入 3.文件上传:短标签绕过php过滤 刷题记录:[CISCN20 ...
- pxe问题
可能镜像路径问题 https://blog.csdn.net/geek_tank/article/details/69479196 一.vmlinuz vmlinuz是可引导的.压缩的内核.“vm”代 ...
- CEF拦截js层alert弹窗 OnJSDialog 《转》
一 引言 CEF3嵌入后,用JS 弹出Alert框,按钮错位,确定按钮勉强能看到.很难看.为了改善体验,决定重写提示框. 环境:VS2008 VC MFC. 二 原理 参看类 CefJSDia ...
- gogs 邀请协作者 500错误
触发原因: 对db文件的user表删了某个用户导致 解决: 注册个新用户,把id改成原来的id(默认都会自增长)
- easyui datagrid怎么动态获取表头的列名及显示名称
说明:目前使用easyui combobox多选属性,绑定的数据源是来自datagrid的表头的列名及显示名称 处理方法: //获取冻结的数据源并返回key,value格式数据 var GetFroz ...