zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    七月份文章收藏
    五月份文章收藏
    二月份文章收藏
    一月份文章收藏
    nmap数据流
    memcached未授权
    Redis未授权漏洞利用方式
    requests sslerror
    欢迎来到Curl的世界
    Dig命令解析结果
  • 原文地址:https://www.cnblogs.com/qiye5757/p/14470719.html
Copyright © 2011-2022 走看看