当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- [wechat applet: cache operation]
- Opencv converts 16 bit image data to 8 bits and 8 to 16
- 说一个软件创业项目,有谁愿意投资的吗?
- selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
- Category of IP address
- mysql分区讲解及操作语句
- Database storage - table partition
- oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
- let const
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
猜你喜欢
Introduction to data fragmentation
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
Data type - floating point (C language)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
How to realize sliding operation component in fast application
Greenplum6.x常用语句
Quick sorting (detailed illustration of single way, double way, three way)
MySQL主从延迟的解决方案
LeetCode 715. Range 模块
随机推荐
[南京大学]-[软件分析]课程学习笔记(一)-introduction
Problems encountered in the use of go micro
Greenplum6.x监控软件搭建
更改当前文件夹及文件夹下文件日期shell脚本
【踩坑】nacos注册一直连接localhost:8848,no available server
LeetCode 715. Range 模块
JEditableTable的使用技巧
Three usage scenarios of annotation @configurationproperties
如何在HarmonyOS应用中集成App Linking服务
Arm GIC (IV) GIC V3 register class analysis notes.
Redis fault handling "can't save in background: fork: cannot allocate memory“
About using CDN based on Kangle and EP panel
JS的操作
leetcode134. gas station
为什么要选择云原生数据库
Mountaineering team (DFS)
Golan idea IntelliJ cannot input Chinese characters
opencv 将16位图像数据转为8位、8转16
Redis summary
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error