LeetCode 804 唯一摩尔斯密码词
Posted Island
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 804 唯一摩尔斯密码词相关的知识,希望对你有一定的参考价值。
package com.lt.datastructure.Set; import java.util.TreeSet; /* * 一个摩斯码,对应一个字母。返回我们可以获得所有词不同单词翻译的数量。 * 遍历字符串,word.charAt(i)-‘a‘获得当前字符所对应的索引,添加到StringBuilder容器。 * 用集合去重 * 返回集合size */ public class LeetCode804 { public static 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‘]); } System.out.println(res.toString()); set.add(res.toString()); } return set.size(); } public static void main(String[] args) { String[] words = {"abbc","axxx","bbcs"}; System.out.println(uniqueMorseRepresentations(words)); } }
以上是关于LeetCode 804 唯一摩尔斯密码词的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 804 唯一摩尔斯密码词[map] HERODING的LeetCode之路