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 ...
随机推荐
- view事件分发源码理解
有些困难无法逃避,没办法,那就只有去解决它.view事件分发对我而言是一块很难啃的骨头,看了<安卓开发艺术探索>关于这个知识点的讲解,看了好几遍,始终不懂,最终通过调试分析结果,看博客,再 ...
- Vue中el-form标签中的自定义el-select下拉框标签
页面写死el-select下拉框标签: 通过v-for="item in stateArr"绑定,stateArr声明在Vue组件里面的data参数里面代码如下: <el-f ...
- stand up meeting 11/24/2015
part 组员 今日工作 工作耗时/h 明日计划 计划耗时/h 词典接口及数据转换 冯晓云 规范在线查词的各项请求,将返回结果解析成树状,并定义完成各种操作以方便其他部分完成调用,排序,增删等操作 3 ...
- 2019CCPC-江西省赛(重现赛)- 感谢南昌大学
A题: 题意: 给你两棵树,然后用一条边将这两棵树连接起来,然后计算 每两点之间的距离,然后求和,问这个和的最小值. 思路:根据重心的性质,树上的所有点到重心的距离最短,因此我们找到两棵树的重心,然后 ...
- G - GCD and LCM 杭电
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, sa ...
- 【原创干货】大数据Hadoop/Spark开发环境搭建
已经自学了好几个月的大数据了,第一个月里自己通过看书.看视频.网上查资料也把hadoop(1.x.2.x).spark单机.伪分布式.集群都部署了一遍,但经历短暂的兴奋后,还是觉得不得门而入. 只有深 ...
- XML-解析失败原因初步分析
更多精彩文章请关注公众号『大海的BLOG』 首先放出有问题的代码 之所以直入主题是因为肝完了事情,急需入睡.hiahia hiboard:updateUrl="https://xxx.com ...
- 支付宝开源非侵入式 Android 自动化测试工具 Soloπ
Soloπ(SoloPi)是支付宝开源的一个无线化.非侵入式的Android自动化测试工具,公测版拥有录制回放.性能测试.一机多控三项主要功能,能为测试开发人员节省宝贵时间. 本文是SoloPi团队关 ...
- python爬虫实战之爬取智联职位信息和博客文章信息
1.python爬取招聘信息 简单爬取智联招聘职位信息 # !/usr/bin/env python # -*-coding:utf-8-*- """ @Author ...
- tp3.2的__construct和_initialize方法
在tp3.2框架里面,有一个php自带的__construct()构造函数和tp3自带的构造函数_initialize()的实行顺序是先实行 php自带的__construct()构造函数 再实行 t ...