- BSTSet存储节点是有序集合,类似TreeSet
- LinkedListSet存储节点是无序集合,类似HashSet
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] codecs = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
// 不同的翻译
Set<String> diffTrans = new TreeSet<>();
for(String word : words){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < word.length(); i++){
sb.append(codecs[word.charAt(i) - 'a']);
}
diffTrans.add(sb.toString());
}
return diffTrans.size();
}
}
基于BST的实现要比基于LinkedList时间复杂度低很多
- TreeMap基于平衡二分搜索树实现(红黑树)
- HashMap基于哈希表实现