804. Unique Morse Code Words
Description
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去重,返回摩斯密码个数(size());
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] code = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};// store the morse code of 'a' ~ 'z'
Set<String> morseSet = new HashSet<>();// 去重计算个数
for(String word: words){
char[] chars = word.toCharArray();// String转化为字符数组
String morse = "";
for(char c: chars){// 对于字符数组的每一个字符,Morse+该字符的Morsecode
morse += code[c - 'a'];
}
morseSet.add(morse);// 该字符串的Morse code如set
}
return morseSet.size();
}
}
804. Unique Morse Code Words的更多相关文章
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- 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 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 ...
- [LeetCode&Python] Problem 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- docker-compose.yml(1)
docker-compose 常用命令 Commands: build Build or rebuild services bundle Generate a Docker bundle from t ...
- LVM-COW写实备份
[root@localhost ~]# fdisk -l /dev/sdb /dev/sdc | grep "LVM"/dev/sdb1 1 9660 77593918+ 8e L ...
- django rest framework ViewSets & Routers
Using viewsets views.py from rest_framework import viewsets from rest_framework import mixins from r ...
- 纯原生JS大图轮播
CSS部分: CSS: <style type="text/css"> #banner { position: relative; width: 500px; heig ...
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
在使用下面这个写法时候UTF-8文件编码 在读取时候出现乱码问题. File myFile=new File("文件路径"); BufferedReader in = new Bu ...
- Python中pandas dataframe删除一行或一列:drop函数
用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False) 参数说明:labels 就是要删除的行列的 ...
- Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task
传送门 https://www.cnblogs.com/violet-acmer/p/10068786.html 题意: 给定一个长度为 n 的数组a[ ],并且有两种操作: ①将前 i 个数全都加上 ...
- typescript和coffeescript简介
typescript 1.什么是typescript?和JavaScript有什么关系? 1.它是一门独立的语言,实现了自己的编译器 2.它的目标代码是JavaScript(很多语言的目标代码是机 ...
- jenkins学习:jenkins+maven
之前配置了jenkins+gitlab,jenkins已经可以从gitlab上面拉取代码了,执行项目构建还要加一个maven. 1.配置全局的maven 系统管理-全局工具配置-配置maven的地址 ...
- 新建工程时报错(26, 13) Failed to resolve: com.android.support:appcompat-v7:28.+ ,
allprojects { repositories { jcenter() maven { url "https://maven.google.com" } } }