标签:class ati ons present 一个 bsp string leetcode char
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)); } }
标签:class ati ons present 一个 bsp string leetcode char
原文地址:https://www.cnblogs.com/ltfxy/p/10010159.html