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).希尔 ...
随机推荐
- [USACO12MAR]花盆 二分 单调队列
[USACO12MAR]花盆 二分 单调队列 存在一个长度为\(x\)的区间\([l,r]\),使得区间中最大值与最小值差至少为\(w\),求这个最小的\(x\) \(n\le 100000\),\( ...
- Android程序员问答题
前言 最近三个月内,不断地进行移动应用开发在线测试题,也积累了不一样的知识.这也将对android studio有很好的掌握,对将来面试也很有好处.那么我就分享给大家.分享是一种幸福,这是一种质的飞越 ...
- mysql 创建时间字段
alter table table1 add order_date datetime null; mysql> select * from table1; +----------+------- ...
- 高斯混合模型(GMM)及MATLAB代码
之前在学习中遇到高斯混合模型,卡了很长一段时间,在这里记下学习中的一些问题以及解决的方法.希望看到这篇文章的同学们对高斯混合模型能有一些基本的概念.全文不废话,直接上重点. 本文将从以下三个问题详解高 ...
- Qt源码学习之路(2) QCoreApplication(1)
QCoreApplication最重要的函数便是exec(),我们便从这个函数开始分析QCoreApplication都干了什么. 先列出exec()函数的源码 static int exec();/ ...
- C# default(T)关键字
C#关键词default函数,default(T)可以得到该类型的默认值. C#在类初始化时,会给未显示赋值的字段.属性赋上默认值,但是值变量却不会. 值变量可以使用默认构造函数赋值,或者使用defa ...
- String.format()详细用法
String.format()字符串常规类型格式化的两种重载方式 format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字 ...
- linux中环境变量和系统加载环境变量的顺序
一.系统环境变量: /etc/profile :这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HOSTNAME, HISTSIZE, uma ...
- matlab学习笔记8 基本绘图命令-特殊图形绘制
一起来学matlab-matlab学习笔记8 基本绘图命令_3 特殊图形绘制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等 ...
- Python - Django - 使用 Bootstrap 样式修改注册页
reg2 函数: from django.shortcuts import render, HttpResponse from app01 import models def reg2(request ...