13.LeetCode 804. Unique Morse Code Words

題目: 

 

 

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        TreeSet<String> set = new TreeSet<String>();//基於平衡二叉樹實現
        for(String word : words){
            StringBuilder ret = new StringBuilder();
            for(int i= 0;i<word.length();i++){
                ret.append(codes[word.charAt(i)-'a']);
            }
            set.add(ret.toString());
        }
        return set.size();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章