LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)

题目标签:String

  题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合。

  然后只要遍历 words,把每一个word 转换成 morse code,把 唯一的 存入 HashSet 就可以了,最后返回 set 的 size。 

Java Solution:

Runtime beats  99.77% 

完成日期:07/02/2018

关键词:HashSet

关键点:把唯一morse code 存入 set

 1 class Solution 
 2 {
 3     public int uniqueMorseRepresentations(String[] words) 
 4     {
 5         String[] mcLetters = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
 6         
 7         Set<String> result = new HashSet<>();
 8         
 9         for(String word: words)
10         {
11             char[] arr = word.toCharArray();
12             String mc = "";
13             
14             for(char c: arr)
15             {
16                 mc += mcLetters[c - 'a'];
17             }
18 
19             result.add(mc);
20         }
21         
22         return result.size();
23     }
24 }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

猜你喜欢

转载自www.cnblogs.com/jimmycheng/p/9352759.html