面试题 01.01. 判定字符是否唯一

  1. 实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
  2. 示例 1
  3. 输入: s = "leetcode"
  4. 输出: false
  5. 示例 2
  6. 输入: s = "abc"
  7. 输出: true
  8. 限制:
  9. 0 <= len(s) <= 100
  10. 如果你不使用额外的数据结构,会很加分。
  11. 来源:力扣(LeetCode
  12. 链接:https://leetcode-cn.com/problems/is-unique-lcci
  13. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

时间:5min
答案

  1. class Solution:
  2. def isUnique(self, astr: str) -> bool:
  3. b= 0
  4. for s in astr:
  5. index= ord(s)-67
  6. if b>>index &1==1:
  7. return False
  8. b= b | 1<<index
  9. return True

要点:
1. 这题练的是位运算,还是挺简单的

面试题 01.02. 判定是否互为字符重排

  1. 给定两个字符串 s1 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,
  2. 能否变成另一个字符串。
  3. 示例 1
  4. 输入: s1 = "abc", s2 = "bca"
  5. 输出: true
  6. 示例 2
  7. 输入: s1 = "abc", s2 = "bad"
  8. 输出: false
  9. 说明:
  10. 0 <= len(s1) <= 100
  11. 0 <= len(s2) <= 100
  12. 来源:力扣(LeetCode
  13. 链接:https://leetcode-cn.com/problems/check-permutation-lcci
  14. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

时间:1min
答案

  1. from collections import Counter
  2. class Solution:
  3. def CheckPermutation(self, s1: str, s2: str) -> bool:
  4. a= Counter(s1)
  5. for s in s2:
  6. if s in a:
  7. a[s]-=1
  8. if a[s]<0:return False
  9. else:
  10. return False
  11. return True

要点

  1. 不知道这题在出什么,对于python都是一行的事情。

:

  1. a

时间:5min
答案

  1. a

要点

  1. 这题练的是位运算,还是挺简单的