当前位置:网站首页>模拟卷Leetcode【普通】1706. 球会落何处
模拟卷Leetcode【普通】1706. 球会落何处
2022-07-07 06:18:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
1706. 球会落何处
用一个大小为 m x n 的二维网格 grid 表示一个箱子。你有 n 颗球。箱子的顶部和底部都是开着的。
箱子中的每个单元格都有一个对角线挡板,跨过单元格的两个角,可以将球导向左侧或者右侧。
将球导向右侧的挡板跨过左上角和右下角,在网格中用 1 表示。
将球导向左侧的挡板跨过右上角和左下角,在网格中用 -1 表示。
在箱子每一列的顶端各放一颗球。每颗球都可能卡在箱子里或从底部掉出来。如果球恰好卡在两块挡板之间的 “V” 形图案,或者被一块挡导向到箱子的任意一侧边上,就会卡住。
返回一个大小为 n 的数组 answer ,其中 answer[i] 是球放在顶部的第 i 列后从底部掉出来的那一列对应的下标,如果球卡在盒子里,则返回 -1 。
示例 1:
输入:grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
输出:[1,-1,-1,-1,-1]
解释:示例如图:
b0 球开始放在第 0 列上,最终从箱子底部第 1 列掉出。
b1 球开始放在第 1 列上,会卡在第 2、3 列和第 1 行之间的 “V” 形里。
b2 球开始放在第 2 列上,会卡在第 2、3 列和第 0 行之间的 “V” 形里。
b3 球开始放在第 3 列上,会卡在第 2、3 列和第 0 行之间的 “V” 形里。
b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 “V” 形里。
示例 2:
输入:grid = [[-1]]
输出:[-1]
解释:球被卡在箱子左侧边上。
示例 3:
输入:grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
输出:[0,1,2,3,4,-1]
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 100
grid[i][j] 为 1 或 -1
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/where-will-the-ball-fall
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def findBall(self, grid: List[List[int]]) -> List[int]:
n = len(grid[0])
res = [-1]*n
for j in range(n):
col = j
for row in grid:
dir = row[col]
col += dir
if col<0 or col==n or row[col]!=dir: break
else:
res[j]=col
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- JS operation
- Basic data types and string types are converted to each other
- Other 7 features of TCP [sliding window mechanism ▲]
- Routing information protocol rip
- FPGA knowledge accumulation [6]
- Greenplum6.x搭建_环境配置
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- Markdown编辑器Editor.md插件的使用
- You should use Google related products with caution
- Tips for using jeditabletable
猜你喜欢

Greenplum6.x搭建_环境配置

MySQL主从延迟的解决方案

数据分析方法论与前人经验总结2【笔记干货】

南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案

Mountaineering team (DFS)

Routing information protocol rip

Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and

如何在快应用中实现滑动操作组件
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]

Input and output of floating point data (C language)
随机推荐
opencv 将16位图像数据转为8位、8转16
Data analysis methodology and previous experience summary 2 [notes dry goods]
let const
Interpolation lookup (two methods)
Greenplum6.x监控软件搭建
Mountaineering team (DFS)
【MySQL】数据库进阶之触发器内容详解
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Gson转换实体类为json时报declares multiple JSON fields named
Teach you how to select PCB board by hand (II)
Three usage scenarios of annotation @configurationproperties
[wechat applet: cache operation]
Redis summary
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
How to realize sliding operation component in fast application
路由信息协议——RIP
opencv之图像分割
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur