Leetcode - 192. 统计词频
解题思路参考代码中的注释:
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2,$1}'
# 1. 首先cat命令查看words.txt
# 2. tr -s ' ' '\n' 将空格都替换为换行,实现分词
# 3. sort 将分好的词按照顺序排序
# 4. uniq -c 统计重复次数(此步骤与上一步息息相关,-c原理是字符串相同则加一,如果不先排序的话将无法统计数目)
# 5. sort -r 将数目倒序排列
# 6. awk '{print $2,$1}' 将词频和词语调换位置打印出来
# 作者:money666
# 链接:https://leetcode-cn.com/problems/word-frequency/solution/yi-bu-yi-bu-ji-suan-ci-pin-by-money666/
查看21道真题和解析