【leetcode】1260. Shift 2D Grid
题目如下:
Given a 2D
gridof sizen*mand an integerk. You need to shift thegridktimes.In one shift operation:
- Element at
grid[i][j]becomes atgrid[i][j + 1].- Element at
grid[i][m - 1]becomes atgrid[i + 1][0].- Element at
grid[n - 1][m - 1]becomes atgrid[0][0].Return the 2D grid after applying shift operation
ktimes.Example 1:
Input:grid= [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]Example 2:
Input:grid= [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]Example 3:
Input:grid= [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]Constraints:
1 <= grid.length <= 501 <= grid[i].length <= 50-1000 <= grid[i][j] <= 10000 <= k <= 100
解题思路:记grid的行数为row,列数为col,显然经过row*col次移动后和不移动效果是一样的,所以可以首先令k = k%(row*col)。剩下的k就是每一个元素需要移动的次数,我的方法是给grid的每个元素编号,从左到右,从上到下,依次为0,1,1....row*col - 1,这样方便计算。
代码如下:
class Solution(object):
def shiftGrid(self, grid, k):
"""
:type grid: List[List[int]]
:type k: int
:rtype: List[List[int]]
"""
row = len(grid)
col = len(grid[0])
k = k%(row * col)
res = [[0] * col for _ in range(row)]
for i in range(row):
for j in range(col):
inx = (i*col + j) + k
if inx >= (row*col):inx -= row*col
newrow = inx/col
newcol = inx%col
res[newrow][newcol] = grid[i][j]
return res
【leetcode】1260. Shift 2D Grid的更多相关文章
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- 重学 html の meta 标签
参考链接: https://segmentfault.com/a/1190000019052062?utm_medium=hao.caibaojian.com&utm_source=hao.c ...
- docker-compose 部署elk+解决时间不对导致kibana找不到logstash定义的index + docker-compose安装
1.拉代码 git clone https://github.com/deviantony/docker-elk.git 2.docker-compose配置文件 [root@host7 docker ...
- Durable NAND flash memory management
词条积累 1.NAND flash memory http://www.searchstorage.com.cn/whatis/word_6052.htm http://baike.baidu.com ...
- MSF魔鬼训练营-3.5.4Nmap与渗透测试数据库
MSF中可以直接使用db_nmap,它是namp的一个封装与NMAP用法完全一致.其执行结果会自动导入至数据库中. 当然也可以在使用 nmap 的 -oX参数输出一个XML格式的文件.这可以是你在本机 ...
- JavaSE编码试题强化练习1
1. 编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 定义父类--圆类 */ public class Circle { / ...
- ZooKeeper常用命令行操作
ZooKeeper常用命令行操作 通过./zkCli.sh 打开zk的客户端进入命令行后台 ls/ls2 列出当前节点下的子节点 ls2还会列出当前节点的状态 [zk: localhost:2181( ...
- Codeforces 1209D Cow and Snacks
题目大意 有 $n$ 个不同的糖果,从 $1$ 到 $n$ 编号.有 $k$ 个客人.要用糖果招待客人. 对于每个客人,这些糖果中恰有两个是其最爱.第 $i$ 个客人最爱的糖果编号是 $x_i$ 和 ...
- P1816忠诚
这是一个区间查询最值的问题,用线段树来做. 建树的时候,这里不是求和,应该是e[k].w=min(e[k*2].w,e[k*2+1].w),所以这里要注意以下,其次是查询的时候,因为本题不用让我们修改 ...
- 从入门到自闭之Python三大器--生成器
1.什么是生成器 核心:生成器的本质就是一个迭代器 迭代器是python自带的的 生成器是程序员自己写的一种迭代器 编写方式: 基于函数编写 推导式编写 def func (): print(&quo ...
- python 写接口供外部调用
.py: import requests import urllib2 import commands import subprocess def check(): status, msg = com ...

