当前位置:网站首页>LeetCode 2351. 第一个出现两次的字母
LeetCode 2351. 第一个出现两次的字母
2022-07-27 23:22:00 【Michael阿明】
1. 题目
给你一个由小写英文字母组成的字符串 s ,请你找出并返回第一个出现 两次 的字母。
注意:
如果 a 的 第二次 出现比 b 的 第二次 出现在字符串中的位置更靠前,则认为字母 a 在字母 b 之前出现两次。
s 包含至少一个出现两次的字母。
示例 1:
输入:s = "abccbaacz"
输出:"c"
解释:
字母 'a' 在下标 0 、5 和 6 处出现。
字母 'b' 在下标 1 和 4 处出现。
字母 'c' 在下标 2 、3 和 7 处出现。
字母 'z' 在下标 8 处出现。
字母 'c' 是第一个出现两次的字母,因为在所有字母中,'c' 第二次出现的下标是最小的。
示例 2:
输入:s = "abcdd"
输出:"d"
解释:
只有字母 'd' 出现两次,所以返回 'd' 。
提示:
2 <= s.length <= 100
s 由小写英文字母组成
s 包含至少一个重复字母
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/first-letter-to-appear-twice
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- set 存储已出现过的字符
class Solution:
def repeatedCharacter(self, s: str) -> str:
occ = set()
for c in s:
if c not in occ:
occ.add(c)
else:
return c
return ''
32 ms 14.9 MB Python3
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
猜你喜欢

idea常用的快捷键汇总

Spool timer

JUC concurrent programming learning

Flutter 通话界面UI

Codeforces暑期训练周报(7.21~7.27)

Oxygen temperature and humidity module

Fabric2.4.4 version building process (complete process)

【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)

Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium

How the test architects of bat factories interpret various disputes of the test platform
随机推荐
Basic learning of cesium
S-RPN: Sampling-balanced region proposal network for small crop pest detection
开发 Flutter 录音功能的插件
Cesium add light sweep
How to make digital retail undertake the development task of the era of traffic and retention may be the key
Thoroughly understand kubernetes scheduling framework and plug-ins
如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
Briefly understand namenode and datanode
测试人员需要了解的软件流程
Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine
Fluent call interface UI
Byte monthly salary 28K, share a wave of my automation testing experience
Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
彻底搞懂kubernetes调度框架与插件
Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors
ICML2022 | 在线决策Transformer
How the test architects of bat factories interpret various disputes of the test platform
S-RPN: Sampling-balanced region proposal network for small crop pest detection
Flutter 通话界面UI