C++解法:

#include <iostream>
#include <vector>
#include <map>
#include <algorithm> using namespace std; class Solution {
public:
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
vector<vector<int>> res(R*C, vector<int>());//数组第三个位置存放两点的曼哈顿距离
int num = ;
for (int i = ; i<C; i++)
{
for (int j = ; j<R; j++)
{
res[num][] = j;
res[num][] = i;
res[num][] = abs(r0 - j) + abs(c0 - i);//曼哈顿距离
num++;
}
}
sort(res.begin(), res.end(), ismax);//排序
for (int i = ; i<num; i++)//将曼哈顿距离删除
{
res[i].pop_back();
}
return res;
}
static bool ismax(vector<int> &a, vector<int> &b)//根据曼哈顿距离升序排序
{
return a[]<b[];
}
}; int main()
{
Solution m;
m.allCellsDistOrder(, , , ); return ;
}

Python解法:

def allCellsDistOrder(R, C, r0, c0):
dist_list = [[] for i in range(R+C)]
for i in range(R):
for j in range(C):
distinct = abs(r0 - i) + abs(c0 - j)
dist_list[distinct].append([i, j])
result = []
for i in dist_list:
if i:
result.extend(i)
else:
break
return result print(allCellsDistOrder(2, 3, 1, 2))

leetcode- 距离顺序排序矩阵单元格的更多相关文章

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

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

  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. PyQt5单元格操作大全

    1.显示二维列表数据(QTableView)控件 '''显示二维列表数据(QTableView)控件数据源model需要创建一个QTableView实例和一个数据源model,然后将其两者关联 MVC ...

  4. count、counta函数巧妙运用于合并单元格填充序号

    函数运用: 1.COUNT(value1,value2, ...)      value1 是必需参数. 要计算其中数字的个数的第一项.单元格引用或区域.      value2, ... 为可选参数 ...

  5. Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值

    Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DB ...

  6. 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数

    // 二维数组中的查找,杨氏矩阵在一个二维数组中.每行都依照从左到右的递增的顺序排序. // 每列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个数组和一个数.推断数组中是否包括这个数 #i ...

  7. WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序

    TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...

  8. JQ完成表格单元格顺序的上移下调

    如有指教及疑问,欢迎留言 HTML代码 <table class="exampletable"> <thead> <tr> <th> ...

  9. 实现GridControl的行单元格非顺序跳转

    用GridControl控件添加数据的时候发现,有一些字段过多但是并不是每个字段都需要用户输入,每个单元格都回车跳转的时候不仅浪费时间,而且用户体验也不好,就需要单元格跳转的时候,不需要的字段可以隔过 ...

随机推荐

  1. 【记录】Nginx错误could not build the server_names_hash you should increase server_names_hash_bucket_size: 32

    今天遇到这个错误,现记录下解决方案: 在nginx的配置文件的http段中增加如下配置: server_names_hash_bucket_size 64; 下面是nginx官方文档解释: 如果定义了 ...

  2. k8s容器-运维管理篇

    二. 运维和管理 维护参考网址 https://jimmysong.io/kubernetes-handbook/practice/install-kubernetes-on-centos.html ...

  3. [转]【Git】rebase 用法小结

    https://www.jianshu.com/p/4a8f4af4e803 本文主要参考 https://git-scm.com/docs/git-rebase rebase在git中是一个非常有魅 ...

  4. C#开发activex

    https://www.cnblogs.com/bobshieh/p/5746844.html

  5. webpack配置(使用react,es6的项目)

    const path = require('path');const webpack = require('webpack');const HtmlWebpackPlugin = require('h ...

  6. fastDFS配置文件 fdfs_client.conf

    # connect timeout in seconds# default value is 30sconnect_timeout=30 # network timeout in seconds# d ...

  7. idea创建Maven项目后启动报404

    这块的配置是

  8. bzoj 2561

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2561 考虑做mst的时候,什么时候这条边不在这棵mst上呢? 就是比他小的权值的边讲这条边的 ...

  9. 【LeetCode 16】最接近的三数之和

    题目链接 [题解] 上一道题那个算法求三个数的和为0的时候,其实就是一个不断在逼近本题中x=0的情况. 那么就套用上面那道题的做法. 在逼近的时候,取个差值的最小值就好了. [代码] class So ...

  10. 最常用的C++序列化方案:protobuf

    参考链接:最常用的两种C++序列化方案的使用心得(protobuf和boost serialization) [c++] Google Protobuf库1. 什么是序列化?程序员在编写应用程序的时候 ...