内容简介:给予一个编码表,26 个字母分别对应一个编码,给定一组单词,获取单词的所有字母组合后的编码中不重复的数量。首先为每个单词的每个字符进行转码, 将转码后的数据放到 Set 集合中, 最后返回 Set 的长度。
给予一个编码表,26 个字母分别对应一个编码,给定一组单词,获取单词的所有字母组合后的编码中不重复的数量。
解法
首先为每个单词的每个字符进行转码, 将转码后的数据放到 Set 集合中, 最后返回 Set 的长度。
class Solution {
private String[] codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
public int uniqueMorseRepresentations(String[] words) {
HashSet<String> hashSet = new HashSet<String>();
for (String word : words) {
hashSet.add(convertCode(word));
}
return hashSet.size();
}
private String convertCode(String word) {
char[] chars = word.toCharArray();
String code = "";
for (char ch : chars) {
code += codes[ch - 97];
}
return code;
}
}
Runtime: 4 ms, faster than 100.00% of Java online submissions for Unique Morse Code Words.
以上所述就是小编给大家介绍的《LeetCode 804 Unique Morse Code Words》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Hit Refresh
Satya Nadella、Greg Shaw / HarperBusiness / 2017-9-26 / USD 20.37
Hit Refresh is about individual change, about the transformation happening inside of Microsoft and the technology that will soon impact all of our lives—the arrival of the most exciting and disruptive......一起来看看 《Hit Refresh》 这本书的介绍吧!