leetcode-第14周双周赛-1274-矩形内船只的数目
2024-09-06 02:03:06
题目描述:
自己的提交:
# """
# This is Sea's API interface.
# You should not implement it, or speculate about its implementation
# """
#class Sea(object):
# def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool:
#
#class Point(object):
# def __init__(self, x: int, y: int):
# self.x = x
# self.y = y class Solution(object):
def countShips(self, sea: 'Sea', topRight: 'Point', bottomLeft: 'Point') -> int:
if topRight.x == bottomLeft.x and topRight.y == bottomLeft.y and sea.hasShips(topRight,bottomLeft):
return 1
if sea.hasShips(topRight,bottomLeft):
mid_h = (topRight.y + bottomLeft.y)//2
mid_w = (topRight.x + bottomLeft.x)//2
a = self.countShips(sea,Point(mid_w,topRight.y),Point(bottomLeft.x,mid_h+1)) if mid_h != topRight.y else 0
b = self.countShips(sea,topRight,Point(mid_w+1,mid_h+1)) if mid_h != topRight.y and mid_w != topRight.x else 0
c = self.countShips(sea,Point(mid_w,mid_h),bottomLeft)
d = self.countShips(sea,Point(topRight.x,mid_h),Point(mid_w+1,bottomLeft.y)) if mid_w != topRight.x else 0
return a + b + c + d
return 0
最新文章
- 【iOS开发】在 App 中加入 AdMob 广告 - 入门介绍与编程技巧
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
- Redis集合-Set
- 浅析配置更快的Eclipse方法
- mysql无法远程连接的解决方法
- Unity 全面理解加载和内存管理
- php之curl实现http与https请求的方法
- mongo常用命令
- TFS服务器(微软源代码管理服务器)上彻底删除项目
- NSObject中的isa到底是个什么?
- git在windows常用命令
- Snail—ORACLE基础之事务学习(五)
- jquery animate 详解
- laravel 简单的上传图片
- Spring-Data-JPA整合MySQL和配置
- 【机器学习】--xgboost初始之代码实现分类
- Linux服务器初步配置流程
- 6-2 铁轨 uva 514
- elastastic search
- all index range ref eq_ref const system 索引type说明