当前位置:网站首页>LeetCode笔记:Weekly Contest 303
LeetCode笔记:Weekly Contest 303
2022-07-26 18:03:00 【Espresso Macchiato】
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题思路倒是也简单,只要找到第一个重复的数字即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def repeatedCharacter(self, s: str) -> str:
cnt = defaultdict(int)
for ch in s:
cnt[ch] += 1
if cnt[ch] > 1:
return ch
提交代码评测得到:耗时37ms,占用内存13.8MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题我的思路非常的暴力,就是把每一行和每一列的元素全部记录下来,然后比较一下求个积即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
n = len(grid)
rows = defaultdict(int)
cols = defaultdict(int)
for i in range(n):
rows[tuple(grid[i])] += 1
cols[tuple([x[i] for x in grid])] += 1
res = 0
for row in rows:
res += rows[row] * cols[row]
return res
提交代码评测得到:耗时809ms,占用内存18.9MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题首先就是我们先存储下每一个食物的分类和评分,然后对于每一个分类,都维护一个按照rating进行排列的数组。
然后,我们每次修改评分之后只需要同步地修改两个数组即可。
2. 代码实现
给出python代码实现如下:
class FoodRatings:
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
self.foods = {
}
self.cuisines = defaultdict(list)
for f, c, r in zip(foods, cuisines, ratings):
self.foods[f] = (r, c)
bisect.insort(self.cuisines[c], (-r, f))
def changeRating(self, food: str, newRating: int) -> None:
oldRating, cuisine = self.foods[food]
self.foods[food] = (newRating, cuisine)
idx = bisect.bisect_left(self.cuisines[cuisine], (-oldRating, food))
self.cuisines[cuisine].pop(idx)
bisect.insort(self.cuisines[cuisine], (-newRating, food))
return
def highestRated(self, cuisine: str) -> str:
return self.cuisines[cuisine][0][1]
提交代码评测得到:耗时1245ms,占用内存44MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题同样是一个巧题,问题的关键就在于说a&b与a|b的各个位数当中的1的个数之和恰好就是a和b当中的1的位数之和。
因此,这道题就变得比较简单了。
2. 代码实现
给出python代码实现如下:
class Solution:
def countExcellentPairs(self, nums: List[int], k: int) -> int:
nums = list(set(nums))
cnt = [0 for _ in range(32)]
for x in nums:
d = 0
while x != 0:
d += x % 2
x = x // 2
cnt[d] += 1
res = 0
for i in range(32):
for j in range(32):
if i + j >= k:
res += cnt[i] * cnt[j]
return res
提交代码评测得到:耗时2884ms,占用内存32.1MB。
边栏推荐
- Kubectl common commands and simple explanations
- MapReduce(二)
- Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
- What should we do after the PMP Exam is postponed on July 30?
- 【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
- JS question brushing plan - linked list
- MySQL数据库命令大全
- 简述MES系统的11大核心功能模块
- Multi merchant mall system function disassembly Lecture 16 - platform side member growth value record
- JS刷题计划——链表
猜你喜欢
![[postgraduate entrance examination vocabulary training camp] day 13 - reliance, expert, subject, unconscious, photograph, exaggeration, counter act](/img/9c/0e6e8abebfd3afdeef2913281a6ada.png)
[postgraduate entrance examination vocabulary training camp] day 13 - reliance, expert, subject, unconscious, photograph, exaggeration, counter act

Learn UML system modeling from me

LeetCode简单题之验证回文字符串 Ⅱ

我酷故我在

The first letter of leetcode simple question appears twice

Tensor Rt的int8量化原理

Operations research 69 | explanation of classic examples of dynamic planning

MySQL - multi table query and case explanation

MySQL learning notes -2. how to improve the query performance of SQL statements

分布式事务-seata
随机推荐
Daorayaki | product principles of non-financial decentralized application
模型定义#pytorch学习
[soft exam] soft exam tutorial + real questions over the years
VPC nat (Sant, nant) experiment
2022 welder (elementary) operation certificate examination question bank and simulation examination
分布式事务-seata
Covos: no need to decode! Semi supervised Vos acceleration using motion vectors and residuals of compressed video bitstreams (CVPR 2022)
LeetCode简单题之验证回文字符串 Ⅱ
Current occupation, write later
5 best overseas substitutes for WPS Office
Redis learning notes-2. Use of the client
2022上海市安全员C证操作证考试题库模拟考试平台操作
MySQL练习题初级45题(统一表)
Meta Cambria handle exposure, active tracking + multi tactile feedback scheme
The first ABAP ALV reporter construction process
[swoole series 3.1] have you been asked about processes, threads, and collaborations during the interview?
LeetCode简单题之装满杯子需要的最短总时长
2022g1 industrial boiler stoker certificate question bank and simulation examination
手写一个Starter
手机申请公募reits账户安全吗?