LeetCode-804-独一无二的莫尔斯码

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

每个字符串分别对应着26个字母,输入一个字符串数组,输出为有几个不重复的莫尔斯码

思路:可以考虑使用HashSet来存储字符串数组的莫尔斯码,因为HashSet是不允许存放重复元素的

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] s = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        Set<String> hs = new HashSet<>();
        for(String word:words){
            StringBuilder sb = new StringBuilder();
            for(char c:word.toCharArray())
                sb.append(s[c-'a']);
            hs.add(sb.toString());
        }
        return hs.size();
    }
}

猜你喜欢

转载自blog.csdn.net/wo8vqj68/article/details/83591152