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. CSS文本元素

    一.属性 font-size:16px;  文字大小 Font-weight: 700 ;   值从100-900,文字粗细,不推荐使用font-weight:bold; Font-family:微软 ...

  2. string拼接时去掉最后一个逗号

     str.replace(str.length() - 1, str.length(), "");

  3. PhpStorm 设置自动FTP同步文件

    1.添加一个FTP服务器 ① 首先在这里打开添加FTP的页面,步骤,工具栏 -> Tools -> Deployment -> Configuration .     ②添加服务器  ...

  4. MongoDB 数据库创建删除

    在MongoDB数据库里面是存在有数据库的概念,但是没有模式(所有的信息都是按照文档保存的),保存数据的结构就是JSON结构,只不过在进行一些数据处理的时候才会使用到MongoDB自己的一些操作符号 ...

  5. 新版本Mariadb安装后相关问题的解决

    给新机器Ubuntu安装的Mariadb后无法登录,通过网上各种方法修改root用户密码,仍然无法解决,耗费几个小时! 经过看日志和查手册,发现原因如下: ubuntu确实安装没有启用root用户,所 ...

  6. NIO Channel Socket套接字相关Channel

    阻塞非阻塞: NIO中的Channel主要分为两大类:一类是FileChannel,另一类是SocketChannel.NIO提供的核心非阻塞特性主要针对SocketChannel类,全部socket ...

  7. 刷题记录:[CISCN2019 东北赛区 Day2 Web3]Point System

    目录 刷题记录:[CISCN2019 东北赛区 Day2 Web3]Point System 知识点 1.padding-oracle attack 2.cbc字节翻转攻击 3.FFMpeg文件读取漏 ...

  8. 大数据 | 分布式文件系统HDFS 练习

    本次作业来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 利用Shell命令与HDFS进行交互 以”./bin/dfs ...

  9. 006 vue监控

    一:监视 1.使用keyup <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  10. RabbitMQ整合Spring Booot【点对点模式】

    pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...