zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    

    reference
    https://leetcode.com/problems/unique-morse-code-words/discuss/120675/C%2B%2BJavaPython-Easy-and-Concise-Solution

    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,比如 0^1=1,0^0=0;
    2.注意数组长度与数组最后一个元素下标的关系;

  • 相关阅读:
    linux分区
    MySQL
    RGB中的颜色的设置
    解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
    解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题
    解决pycharm下安装reportLab报错的问题
    简单的爬取并下载图片的程序
    linux常用命令
    ubuntu下安装pycharm的方法
    win7下装ubuntu双系统后无法进入win7的解决方法
  • 原文地址:https://www.cnblogs.com/victorxiao/p/11101311.html
Copyright © 2011-2022 走看看