博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode-804-Unique Morse Code Words
阅读量:7118 次
发布时间:2019-06-28

本文共 1970 字,大约阅读时间需要 6 分钟。

题目描述:

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: 2Explanation: The transformation of each word is:"gin" -> "--...-.""zen" -> "--...-.""gig" -> "--...--.""msg" -> "--...--."There are 2 different transformations, "--...-." and "--...--.".

 

Note:

  • The length of words will be at most 100.
  • 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
& words) { set
res;//存储翻译之后的摩尔斯码 vector
morse={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; int s1=words.size(),s2; string t;//临时变量 for(int i=0;i

上述代码逻辑清晰,实测6ms,beats 99.70% of cpp submissions。

转载于:https://www.cnblogs.com/chenjx85/p/9050380.html

你可能感兴趣的文章
我的友情链接
查看>>
思科路由器 DHCP配置
查看>>
cacti安装的一个错误
查看>>
四:Cocos2d-x设计思想
查看>>
java8之Stream API(提取子流和组合流)
查看>>
工大瑞普启动器
查看>>
我的友情链接
查看>>
mongodb,spring data api常用总结
查看>>
常用端口知识汇总
查看>>
outlook客户端接收邮件报错0x80040600
查看>>
EditPlus注册码 亲测最新版可用
查看>>
如何消除Windows 7中的搜索记录
查看>>
request.getParameterMap()使用
查看>>
挂载硬盘报错无法挂载、分区只读的解决方法
查看>>
xFire两种客户端的传递参数
查看>>
python版判断IP地址
查看>>
Sublime text2空格替换tab键
查看>>
linux中脚本后台执行的方法
查看>>
Python爬虫入门教程 6-100 蜂鸟网图片爬取之一
查看>>
我的友情链接
查看>>