当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Implementation of navigation bar at the bottom of applet
- Greenplum 6.x reinitialization
- 对API接口或H5接口做签名认证
- Database storage - table partition
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
- Image segmentation in opencv
- MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
- Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
- Rapid integration of authentication services - harmonyos platform
- Greenplum6.x搭建_环境配置
猜你喜欢

Greenplum6.x搭建_安装
![[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University](/img/e2/519a5267cd5425a83434d2da65ebe6.jpg)
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University

路由信息协议——RIP
![[MySQL] detailed explanation of trigger content of database advanced](/img/6c/8aad649e4ba1160db3aea857ecf4a1.png)
[MySQL] detailed explanation of trigger content of database advanced

Rapid integration of authentication services - harmonyos platform

登山小分队(dfs)

Greenplum 6.x reinitialization

Image segmentation in opencv

Greenplum6.x重新初始化
![[Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/2e/3a7d71a0b5a6aff294a0bd2f8515f8.jpg)
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
随机推荐
Three series of BOM elements
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
You should use Google related products with caution
Enterprise manager cannot connect to the database instance
JEditableTable的使用技巧
对API接口或H5接口做签名认证
年薪50w阿里P8亲自下场,教你如何从测试进阶
Three usage scenarios of annotation @configurationproperties
Go write a program that runs within a certain period of time
数据分片介绍
基本数据类型和string类型互相转化
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
Introduction to data fragmentation
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
数字三角形模型 AcWing 275. 传纸条
Novice entry SCM must understand those things
数字三角形模型 AcWing 1027. 方格取数
QT charts use (rewrite qchartview to realize some custom functions)
Greenplum6.x重新初始化