当前位置:网站首页>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。
边栏推荐
- How to become an excellent test / development programmer? Focus on planning and then move
- 一些时序建模策略(一)
- Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
- Use notes of Lichuang EDA
- Multi thread learning notes -1.cas
- Lombok common notes
- MySQL learning notes -2. how to improve the query performance of SQL statements
- MySQL - function and constraint commands
- LeetCode简单题之第一个出现两次的字母
- OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
猜你喜欢

SSM integration configuration

NFT digital collection development: digital collections help enterprise development

Verification palindrome string II of leetcode simple question

Complete MySQL database commands

Unity farm 2 - planting system

VTK (the Visualization Toolkit) loads STL models

2022上海市安全员C证操作证考试题库模拟考试平台操作
![[swoole series 3.1] have you been asked about processes, threads, and collaborations during the interview?](/img/62/2aa1999f461ea5afd19b78bcd4ded8.jpg)
[swoole series 3.1] have you been asked about processes, threads, and collaborations during the interview?

Leetcode simple question: the minimum total time required to fill a cup

MySQL数据库命令大全
随机推荐
The diagram of user login verification process is well written!
VTK (the Visualization Toolkit) loads STL models
网络协议:TCP/IP协议
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
Covos: no need to decode! Semi supervised Vos acceleration using motion vectors and residuals of compressed video bitstreams (CVPR 2022)
【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
JS question brushing plan - linked list
Racher deploys kubernetes cluster
2022茶艺师(中级)考试题模拟考试题库及答案
OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
NFT digital collection system development: sold out when online, and netizens "spike" Digital Collections
rancher部署kubernetes集群
Paged query design of scenarios
MySQL - 函数及约束命令
How many pairs can an array of leetcode simple questions form
Huawei cloud · cloud sharing experts~
【AUTOSAR-RTE】-1-聊一聊RTE(Run-Time Environment)
Likeshop takeout order system is open source, 100% open source, no encryption
[soft exam] soft exam tutorial + real questions over the years
实用工具网站推荐