LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a"
maps to ".-"
, "b"
maps to "-..."
, "c"
maps to "-.-."
, and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--." There are 2 different transformations, "--...-." and "--...--.". 加班之余水个题~ 利用一下set的唯一性,效率还挺高
class Solution {
public int uniqueMorseRepresentations(String[] words) {
if (words == null)
return 0;
String[] codes = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int len = words.length;
Set<String> set = new HashSet<>();
for (int i=0; i<len; i++) {
String word = words[i];
StringBuilder codeBuilder = new StringBuilder();
for (int j=0; j<word.length(); j++) {
codeBuilder.append(codes[word.charAt(j)-'a']);
}
set.add(codeBuilder.toString());
}
return set.size();
}
}
LeetCode - 804. Unique Morse Code Words的更多相关文章
- LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)
题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode 804 Unique Morse Code Words 解题报告
题目要求 International Morse Code defines a standard encoding where each letter is mapped to a series of ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
随机推荐
- Python中将array类型不按科学计数法存在文件中的方法
直接上代码: from numpy import *import numpy as npDrug_array = zeros((708,708),dtype = int)f = open('D:\ma ...
- JAVA自学笔记11
JAVA自学笔记11 1:Eclipse的安装 2:用Eclipse写一个HelloWorld案例,最终在控制台输出你的名字 A:创建项目 B:在src目录下创建包.cn.itcast C:在cn.i ...
- HTML5 学习01——浏览器问题、新元素
Internet Explorer 浏览器问题 问题:Internet Explorer 8 及更早 IE 版本的浏览器不支持HTML5的方式. <!--[if lt IE 9]> < ...
- APP中的图片如何长按可以下载并保存图片到相册
直接上图 方式一: 实现方式二: 方式三:
- 需要看源码的java类
1.数据结构相关的类,如String.ArrayList,LinkedList,HashMap和ConcurrentHashMap等等.2.线程并发相关的类,如Synchronized.Reentra ...
- SpringBoot项目获取ApplicationContext来GetBean的方法
一.简介 我们开发时,经常遇到有些实例需要动态创建,比如有构造函数的组件等.这时候,Spring时我们有ClassPathXmlApplicationContext,但是在Spring Boot时,我 ...
- Android之Wifi学习(1)
在Android中对Wifi操作,android本身提供了一些实用的包.在android.net.wifi包以下.简介一下: 大致能够分为四个基本的类ScanResult,wifiConfigurat ...
- Effective Java 第三版—— 86. 非常谨慎地实现SERIALIZABLE接口
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- 设置 WPF 的内容支持触摸直接滚动
在滚动内容上设置属性 ScrollViewer.PanningMode 的值即可. 另外可重写 OnManipulationBoundaryFeedback 方法来替换系统默认的滚动到最上最下时触发的 ...
- 【Java】Java8的Lambda入门记录
简化定义匿名实现类 匿名实现类的传统方式 创建一个线程,需要实现Runnable接口,并实现public void run()方法,用传统的方式是这样的: public static void mai ...