LeetCode804. 唯一摩尔斯密码词

 

 

 

import java.util.TreeSet;
class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

        TreeSet<String> set = new TreeSet<>();
        for(String word: words){
            StringBuilder res = new StringBuilder();
            for(int i = 0; i < word.length(); i++){
                res.append(codes[word.charAt(i) - 'a']);               
            }
            set.add(res.toString());
        }
        return set.size();
    }
}
Solution 1

 

posted @ 2020-05-04 22:48  不学无墅_NKer  阅读(165)  评论(0编辑  收藏  举报