LeetCode--Unique Morse Code Words && Flipping an Image (Easy)
804. Unique Morse Code Words (Easy)#
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 "--...--.".
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.
solution#
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] alphamorse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
Set<String> transformation = new HashSet<String>();
for (int i=0; i<words.length; i++)
{
StringBuilder s = new StringBuilder();
for (int j=0; j<words[i].length(); j++)
s.append(alphamorse[words[i].charAt(j)-'a']);
transformation.add(s.toString());
}
return transformation.size();
}
}
总结##
此题思路是先将26个字母的morse code用一个String数组alphamorse存起来,然后再用一个HashSet变量transformation存储转化为morse code后的单词。接着外层循环依次遍历每个单词,此时需要新建一个StringBuilder变量s用来存储每个单词中的字母连接后的morse code。找到一个单词后,内层循环再遍历其每个字母;找到一个字母后,将这个字母与'a'相减,得到一个整数值,这个整数值代表这个字母在alphamorse中的下标。接着用s.append()将morse code存入s,内层循环结束后再s转变为字符串后存入transformation,直至外层循环结束。最后返回transformation的大小,即为所求。
Notes:
1.遍历String数组可以用for-each循环;
2.遍历字符串,可以将字符串转为字符数组后再用for-each循环
eg.
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
HashSet<String> s = new HashSet<>();
for (String word : words) {
String code = "";
for (char c : word.toCharArray()) code += d[c - 'a'];
s.add(code);
}
return s.size();
}
}
832. Flipping an Image (Easy)#
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
Example 1:
Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Notes:
1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1
solution#
我的解法
class Solution {
public int[][] flipAndInvertImage(int[][] A) {
for (int i=0; i<A.length; i++)
{
int j = 0;
int k = A[i].length-1;
while(j < k)
{
int temp = A[i][j];
A[i][j] = A[i][k] ^ 1;
A[i][k] = temp ^ 1;
j++; k--;
}
if (j == k)
A[i][j] = A[i][j] ^ 1;
}
return A;
}
}
大佬的解法
class Solution {
public int[][] flipAndInvertImage(int[][] A) {
int n = A.length;
for (int[] row : A)
for (int i = 0; i * 2 < n; i++)
if (row[i] == row[n - i - 1])
row[i] = row[n - i - 1] ^= 1;
return A;
}
}
reference
https://leetcode.com/problems/flipping-an-image/discuss/130590/C%2B%2BJavaPython-Reverse-and-Toggle
总结##
此题思路很简单,先用一个外层循环遍历每个image,即取出一行,然后在一个内层while循环里面对一行里面的数字逆序,逆序时同时做异或运算。按照我的解法,内层循环结束后,此时要注意一行的长度可能为奇数或偶数,如果是奇数,则还要对一行中正中间的数字做异或运算,否则什么都不做。最后外层循环结束后,返回二维数组A即可。
Notes:
1.a^b为异或运算,相同得0,相异得1,比如 01=1,00=0;
2.注意数组长度与数组最后一个元素下标的关系;
LeetCode--Unique Morse Code Words && Flipping an Image (Easy)的更多相关文章
- [LeetCode] 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
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- Unique Morse Code Words
Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...
- 【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 (唯一摩尔斯密码词)
题目标签: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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
随机推荐
- 如何练习python?有这五个游戏,实操经验就已经够了
现在学习python的人越来越多了,但仅仅只是学习理论怎么够呢,如何练习python?已经是python初学者比较要学会的技巧了! 其实,最好的实操练习,就是玩游戏. 也许你不会信,但这五个小游戏足够 ...
- matlab将数据读取和写入txt文档
原文链接 matlab中打开文件 fid = fopen(文件名,‘打开方式’): 说明:fid用于存储文件句柄值,如果fid>0,这说明文件打开成功. 另外,在这些字符串后添加一个“t”,如‘ ...
- Delphi程序启动参数的读取
Delphi中有两个专门用于读取命令行参数的变量: Paramcount-->用于返回命令行参数的个数 Paramstr数组-->用于返回指定的命令行参数 示例代码 ...
- XML布局界面
Android推荐使用XML布局文件来定义用户界面,而不是使用Java代码来开发用户界面,因此基础所有组件都提供了两种方式来控制组件的行为:1.在XML布局文件中通过XML属性进行控制:2.在Java ...
- ROM定制开发教程-Android adb命令用法与实例解析
一.什么是ADB Android Debug Bridge(adb)是一个命令行工具,可让您与模拟器或连接的Android设备进行通信.您可以在android sdk / platform-tools ...
- EOS基础全家桶(八)jungle测试网的使用
简介 前面我们已经学习了一些EOS的基础知识了,但是在EOS主网上的很多操作(比如:抵押.赎回.买卖内存)都是需要EOS链被正式激活后才可使用,而激活EOS链还需要很多的准备操作,我打算在单独的一篇文 ...
- SringMVC入门程序
Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架 1.Spring优点 轻量级,简单易学 高效 , 基于请求响应的MVC框架 与Spring兼 ...
- 词向量表示:word2vec与词嵌入
在NLP任务中,训练数据一般是一句话(中文或英文),输入序列数据的每一步是一个字母.我们需要对数据进行的预处理是:先对这些字母使用独热编码再把它输入到RNN中,如字母a表示为(1, 0, 0, 0, ...
- golang依赖管理
目录 使用GOPATH管理依赖 临时GOPATH 依赖查找路径 使用GOVENDER管理依赖 使用GO111MODULE管理依赖 Usage 常用命令列表 不常用命令 使用示例 开启GO111MODU ...
- python os模块获取指定目录下的文件列表
bath_path = r"I:\ner_results\ner_results" dir_list1 = os.listdir(bath_path) for dir1 in di ...