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的更多相关文章

  1. 【Leetcode_easy】1030. Matrix Cells in Distance Order

    problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...

  2. [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 & ...

  3. 【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 ...

  4. 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  5. LeetCode.1030-曼哈顿距离排序矩阵单元格(Matrix Cells in Distance Order)

    这是小川的第384次更新,第412篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第246题(顺位题号是1030).我们给出一个矩阵,其中R行和C列具有整数坐标(r,c)的 ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. Leetcode 第133场周赛解题报告

    今天参加了leetcode的周赛,算法比赛,要求速度比较快.有思路就立马启动,不会纠结是否有更好的方法或代码可读性.只要在算法复杂度数量级内,基本上是怎么实现快速就怎么来了. 比赛时先看的第二题,一看 ...

  8. Weekly Contest 133

    1030. Matrix Cells in Distance Order We are given a matrix with R rows and C columns has cells with ...

  9. 算法与数据结构基础 - 排序(Sort)

    排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...

随机推荐

  1. [转]gulp排除已压缩文件思路

    文章转载至[gulp排除已压缩文件思路] gulp默认排除语法的弊端 有个时候我们需要时用gulp排除已经压缩过的js,css等.如果以压缩文件是以".min.js"之类命名规范的 ...

  2. Android中如何引入已经存在的标题栏

    在写程序中很多时候很多页面的标题布局大致都是一样的,这时候不需要挨着都写一遍,只需要写一个xml文件,在所需要的时候进行引用就可以了 只需要这一行代码: <include layout=&quo ...

  3. 让你的shell更体贴

    使用shell的时候很多,特别是拉幕式终端,使用时更加方便,同时可以利用 echo "Did you know that:" ;whatis $(ls /bin | shuf -n ...

  4. 【2019】Charles视频教程,接口测试工具最新教程

    Charles 是在 windows/mac/linux下常用的网络封包截取工具,也是电商/直播/搜索/金融/H5/App等测试专用接口测试工具. Charles 支持Http/Https/Webso ...

  5. js中isNaN和Number.isNaN的区别

    isNaN 当我们向isNaN传递一个参数,它的本意是通过Number()方法尝试将这参数转换成Number类型,如果成功返回false,如果失败返回true. 所以isNaN只是判断传入的参数是否能 ...

  6. MYSQL事务的开启与提交

    MYSQL 事务处理主要有两种方法: 1.用 BEGIN, ROLLBACK, COMMIT来实现 BEGIN 开始一个事务 ROLLBACK 事务回滚 COMMIT 事务确认 2.直接用 SET 来 ...

  7. coredump之栈溢出

    1.栈溢出引发的core往往出现出现在递归调用中. gdb时看到的特征是: 栈缺失,当前栈地址不可读. 根据栈是逆向生长的特点(栈逆向生长,所以很容易出现类似数组溢出覆盖率函数返回地址,导致函数退出地 ...

  8. ArrayList: java之ArrayList详细介绍(转)

    1  ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List ...

  9. android -------- 流式布局,支持单选、多选等

    最近开发中有流式标签这个功能,网上学了下,来分享一下 Android 流式布局,支持单选.多选等,适合用于产品标签等. 效果图: 用法: dependencies { compile 'com.hym ...

  10. apt与apt-get命令的区别

    apt 和 apt-get的区别 Debian 作为 Ubuntu.Linux Mint 和 elementary OS 等 Linux 操作系统的母板,其具有强健的「包管理」系统,它的每个组件和应用 ...