[Leetcode] 每日两题 2013 2047 -day83

2013. 检测正方形

在这里插入图片描述
在这里插入图片描述

使用仨哈希记录,首先记录x下 不同的y 的list

在记录y 下 不同的x 的list

再统计x,y点的个数 因为题目允许重复点

对于 新来的点 a,b

查找在 a下有多少个的y,对于每个y 计算其边长,然后去找相应的x点位,再乘与要找的两个点的数量,累计和

class DetectSquares:

    def __init__(self):
        self.dicx = defaultdict(list)
        self.dicy = defaultdict(list)
        self.dic = defaultdict(int)

    def add(self, point: List[int]) -> None:
        x,y = point[:2]
        self.dicx[x].append(y)
        self.dicy[y].append(x)
        self.dic[(x,y)] +=1
    def count(self, point: List[int]) -> int:
        a,b = point[:2]
        res = 0
        for y in self.dicx[a]:
            dis = abs(b-y)
            if dis >0:
                for x in [a-dis,a+dis]:
                   
                    res += self.dic[(x,b)]*self.dic[(x,y)]
        return res 


2047. 句子中的有效单词数

在这里插入图片描述

判断

class Solution:
    def countValidWords(self, sentence: str) -> int:

        words =sentence.split(' ')
        res = 0
        for word in words:
            flag=1
            glag =0
            if word:
                for a in range(len(word)):
                    if word[a]=='-' and a>0 and a<len(word)-1 and glag == 0 and flag == 1 and ord(word[a-1]) >=ord('a') and ord(word[a-1])<=ord('z')and ord(word[a+1]) >=ord('a') and ord(word[a+1])<=ord('z'):
                        glag =1
                        continue
                    if word[a] in ['!','.',',']and a==len(word)-1 and  flag ==1:
                        continue
                    if  ord(word[a]) <ord('a') or ord(word[a])>ord('z') :
                        flag = 0
                if flag ==1:
                    #print(word)
                    res+=1
        return res

正则

class Solution:
    def countValidWords(self, sentence: str) -> int:

      
    return sum(bool(re.match(r'[a-z]*([a-z]-[a-z]+)?[!.,]?$', w)) for w in sc.split())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值