999. Available Captures for Rook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | class Solution: def calcu( self ,ele: List [ str ],loc): index_p = [ - 1 ] * 2 #只记录离R最近位置的p和B(左右) index_B = [ - 1 ] * 2 left,right = 0 , 0 for i in range ( len (ele)): if ele[i] is 'p' : if i < loc: index_p[ 0 ] = i #左边的取最大值 else : if index_p[ 1 ]< 0 :index_p[ 1 ] = i #右边的取最小值 elif ele[i] is 'B' : if i<loc: index_B[ 0 ] = i else : if index_B[ 1 ]< 0 :index_B[ 1 ] = i p0,p1,b0,b1 = index_p[ 0 ],index_p[ 1 ],index_B[ 0 ],index_B[ 1 ] #使用简便的符号标记,方便书写 if p0> = 0 : #p在R的左边 if b0> = 0 : #B在R的左边,有值 if p0<loc and b0<p0: left = 1 else : #B左边没有值 if p0<loc: left = 1 if p1> = 0 : #p在R的右边 if b1> = 0 : #B在R的右边,有值 if p1>loc and b1>p1: right = 1 else : #B右边没有值 if p1>loc: right = 1 return left,right def numRookCaptures( self , board: List [ List [ str ]]) - > int : index_R_row = 0 index_R_col = 0 count = 0 col = [] row = [] for a in board: index_R_row + = 1 if 'R' in a: for i in range ( len (a)): if a[i] = = 'R' : index_R_col = i col = [x[i] for x in board] row = a break break r1,r2 = self .calcu(row,index_R_col) r3,r4 = self .calcu(col,index_R_row - 1 ) count = r1 + r2 + r3 + r4 return count |
40ms,13M
my god,写这个代码花了我半天时间,估计以后都不想看第二遍了。。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下