• leetcode每日一题之5.猜字谜


    猜字谜

    解法

    首先将word单词进行二进制压缩,重复的单词去掉,组成sword哈希映射。然后循环puzzle,先求出puzzle的所有解(这个可以用leetcode每日一题之4.子集来求),然后在sword中匹配,加上相应的数量。代码如下

    class Solution {
    
        /**
         * @param String[] $words
         * @param String[] $puzzles
         * @return Integer[]
         */
        function findNumOfValidWords($words, $puzzles) {
            // 首先构造相关的数据结构
            $swords = [];
            foreach($words as $word){
                $length = strlen($word);
                $wordValue = 0;
                for($i = 0; $i < $length; $i++){
                    $wordValue |= (1 << ord($word[$i]) - ord('a'));
                }
                if ($this->countOne($wordValue) <= 7){
                    $swords[$wordValue] = isset($swords[$wordValue]) ? $swords[$wordValue] + 1 : 1;
                }
            }
    
            $spuzzle = [];
            foreach($puzzles as $puzzle){
                $total = 0;
                $test = (1 << ord($puzzle[0]) - ord('a'));
                if(array_key_exists($test, $swords)) $total += $swords[$test];
                // 求出谜面所有的解
                $puzzleValue = 0;
                $length = strlen($puzzle);
                $tmps = [$test];
                for($i = 1; $i < $length; $i++){
                    foreach($tmps as $tmp){
                        $test = $tmp | (1 << ord($puzzle[$i]) - ord('a'));
                        if(array_key_exists($test, $swords)) $total += $swords[$test];
                        $tmps[] = $test;
                    }
                }
                $spuzzle[] = $total;
            }
    
            return $spuzzle;
    
        }
    
        function countOne($n){
            $string = decbin($n);
            $number = 0;
            $count = strlen($string);
            for($i = 0; $i < $count; $i++){
                if($string[$i] == 1) $number++;
            }
            return $number;
        }
    }
  • 相关阅读:
    ZMQ面面观
    windows10系统右键新建菜单的自定义
    元组,列表,字典前加*
    HTTP状态码(转)
    字符串利用%02d将月份前加0
    python中while与else的联姻
    sys.argv
    pandas的read_csv踩到的坑
    wireshark抓包总结
    bcolz
  • 原文地址:https://www.cnblogs.com/qiye5757/p/14470719.html
Copyright © 2020-2023  润新知