面试题 01.01. 判定字符是否唯一
实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
示例 1:
输入: s = "leetcode"
输出: false
示例 2:
输入: s = "abc"
输出: true
限制:
0 <= len(s) <= 100
如果你不使用额外的数据结构,会很加分。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/is-unique-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
时间:5min
答案:
class Solution:
def isUnique(self, astr: str) -> bool:
b= 0
for s in astr:
index= ord(s)-67
if b>>index &1==1:
return False
b= b | 1<<index
return True
要点:
1. 这题练的是位运算,还是挺简单的
面试题 01.02. 判定是否互为字符重排
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,
能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-permutation-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
时间:1min
答案:
from collections import Counter
class Solution:
def CheckPermutation(self, s1: str, s2: str) -> bool:
a= Counter(s1)
for s in s2:
if s in a:
a[s]-=1
if a[s]<0:return False
else:
return False
return True
要点:
- 不知道这题在出什么,对于python都是一行的事情。
:
a
时间:5min
答案:
a
要点:
- 这题练的是位运算,还是挺简单的