码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 804 唯一摩尔斯密码词

时间:2018-11-23 23:31:56      阅读:238      评论:0      收藏:0      [点我收藏+]

标签: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));
    }
}

 

LeetCode 804 唯一摩尔斯密码词

标签:class   ati   ons   present   一个   bsp   string   leetcode   char   

原文地址:https://www.cnblogs.com/ltfxy/p/10010159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!