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, "cab" 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 "--...--.".
Note:
- The length of
words
will be at most100
. - Each
words[i]
will have length in range[1, 12]
. -
words[i]
will only consist of lowercase letters.
要完成的函数:
int uniqueMorseRepresentations(vector<string>& words)
说明:
1、这道题给定一个vector,里面存放着多个字符串,字符串中的每一个字母都对应一个摩尔斯码。要求把给定的字符串翻译成摩尔斯码,最后返回有多少种不同的摩尔斯码。
2、明白题意,这道题也没有什么快速的方法来实现,就直接暴力解法做吧。
外层循环,取出每个字符串。
内层循环,取出字符串中的逐个字母,一一对照着翻译成摩尔斯码,最终结果存储在新定义的string中,把string一一加入到set中。
最后返回set的个数就可以了。
代码如下:
int uniqueMorseRepresentations(vector<string>& words)
{
set<string>res;//存储翻译之后的摩尔斯码
vector<string>morse={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int s1=words.size(),s2;
string t;//临时变量
for(int i=0;i<s1;i++)//逐一取出字符串
{
s2=words[i].size();
t="";
for(int j=0;j<s2;j++)//逐一取出字母
t+=morse[words[i][j]-'a'];//翻译成摩尔斯码
res.insert(t);//存储到set中
}
return res.size();
}
上述代码逻辑清晰,实测6ms,beats 99.70% of cpp submissions。
- 运维必备技能 WEB 日志分析
- Elasticsearch 一键安装含中文分词
- Session 的 Cookie 域处理(多域名虚拟主机)
- ElasticSearch + Logstash + Kibana 日志采集
- ElasticSearch + Logstash + Kibana 一键安装
- Oracle 表空间管理
- 数据加密字段加密
- 《Netkiller Virtualization 手札》Docker 卷管理
- PHP高级编程之守护进程
- Spring boot with Docker
- Spring boot with Service
- Spring boot with PostgreSQL
- Struts2 S2-046, S2-045 Firewall(漏洞防火墙)
- 应用程序的通信成本
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 快速学习-Jenkins CLI QuickStart
- 快速学习-Jenkins CLI用户
- 快速学习-Jenkins CLI诊断
- 快速学习-Jenkins CLI下载
- vue-element-admin提交表单数据到后端
- JDK15正式发布,划时代的ZGC同时宣布转正
- Redis的各种数据类型实践---List结构
- Git分支操作方法
- 最全解密微信红包随机算法(含代码实现)
- 两次死锁的分析
- 一次解决你的图像尺寸和定位问题。
- vuedraggable实现列表拖动排序
- 在 Xcode 中添加 Swift package 依赖
- 浅谈JavaScript中的apply,call和bind
- Git 的简单使用