LeetCode804.唯一摩尔斯密码词(Java实现)

链接:https://leetcode-cn.com/problems/unique-morse-code-words/

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
String[] codes={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
		HashSet<String> set=new HashSet<String>();
		for (String word : words) {
			StringBuilder sb=new StringBuilder("");
			for(int i=0;i<word.length();i++){
				sb.append(codes[word.charAt(i)-97]);
			}
			set.add(sb.toString());
		}
		return set.size();
    }
}
发布了88 篇原创文章 · 获赞 142 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u010189239/article/details/89575768