美文网首页
#1. unique chars & permutati

#1. unique chars & permutati

作者: district10 | 来源:发表于2017-12-12 00:22 被阅读5次

发现这个教程不错, 上班之余可以很快看一两个习题.

这里是索引: https://github.com/4ker/interactive-coding-challenges/blob/master/README.md

判定 string 是否无重复字符

https://github.com/4ker/interactive-coding-challenges/blob/master/arrays_strings/unique_chars/unique_chars_solution.ipynb

三个思路:

  1. len(set(input)) == len(input)
  2. 一点一点生成这个 set, 一边 add 一边判断, 而不是整个生成 set 再看 len
  3. 用 foreach, count 每个 char 的个数

看 string 是不是 permutation 置换等价

https://github.com/4ker/interactive-coding-challenges/blob/master/arrays_strings/permutation/permutation_solution.ipynb

思路:

  1. 正则化 (sorted) 看是否相等: sorted(input1) == sorted(input2)
  2. 统计字符, 看各个字符的分布是否一致, 用了 collections 下的 defaultdict (可以直接用 == 判断两个 dict 是否相等)

Python Notes

# new a set and insert
s = set()
s.add(v)

# foreach
for char in string:
    ...

# count char in string
string.count(char) == 1

# literals and reserved words
True, False, if ... and ..., if ... or ...

# defaultdict
from collections import defaultdict
counter = defaultdict(int)
counter['key'] += 1

jupyter notebook 的帮助: 用 ?.

快捷键只要记住一个 command+shift+f: command palette.

相关文章

网友评论

      本文标题:#1. unique chars & permutati

      本文链接:https://www.haomeiwen.com/subject/wbfgixtx.html