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


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去重的步骤。

点赞 取消点赞 收藏 取消收藏

<< 上一篇: Laravel 5.8 模型策略自动解析示例

>> 下一篇: 基于 Laravel 5.7 开发的后台管理系统