首页
技术博客
登录
6mi
u
盘
搜
搜 索
技术博客
leetcode判定字符是否唯一
leetcode判定字符是否唯一
tech
2025-04-19
9
实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
输入: s = “leetcode” 输出: false
address
def
is_unique
(
astr
:
str
)
-
>
bool
:
# 1. 利用集合去重
# return len(astr) == len(set(astr))
# 2. 遍历一遍字符串,如果重复则返回False
buckets
=
[
0
]
*
26
for
char
in
astr
:
buckets
[
ord
(
char
)
-
97
]
+=
1
if
buckets
[
ord
(
char
)
-
97
]
>
1
:
return
False
return
True
转载请注明原文地址:https://tech.qufami.com/read-21689.html
最新回复
(
0
)