Leetcode PHP题解--D5 804. Unique Morse Code Words

0

804. Unique Morse Code Words

题目链接

804. Unique Morse Code Words

题目分析

这个题目要求算出把给定数组中的字符串转换成摩尔斯码后,有多少个不同的摩尔斯码。

思路

第一步需要把字符串转换成摩尔斯码。

$morse = [
    ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
    "-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
];
$replaced = [];
foreach($words as $word){
    $chars = str_split($word);
    $string = '';
    foreach($chars as $char){
        $string .= $morse[ord($char)-ord('a')];
    }
}

转换完成后存进数组内,再用array_unique函数排除。再count排除结果即可。

最终代码

<?php
class Solution {
    function uniqueMorseRepresentations($words) {
        $morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
        $replaced = [];
        foreach($words as $word){
            $chars = str_split($word);
            $string = '';
            foreach($chars as $char){
                $string .= $morse[ord($char)-ord('a')];
            }
            $replaced[] = $string;
        }
        return count(array_unique($replaced));
    }
}

若觉得本文章对你有用,欢迎用爱发电资助。

优化方案

  • 直接存为数组的键则可以省去用array_unique去重的步骤。

你可能感兴趣的

载入中...