|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: linyz |
| 5 | + * Date: 2021/5/30 |
| 6 | + * Time: 22:23 |
| 7 | + */ |
| 8 | + |
| 9 | +class L401_BinaryWatch |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param Integer $turnedOn |
| 13 | + * @return String[] |
| 14 | + */ |
| 15 | + public $minRes = array(); |
| 16 | + public $hourRes = array(); |
| 17 | + |
| 18 | + function readBinaryWatch($turnedOn) { |
| 19 | + $allot = array(); |
| 20 | + for($i = 0;$i <= $turnedOn;$i ++){ |
| 21 | + if($i > 4 || ($turnedOn - $i) > 6){ |
| 22 | + continue; |
| 23 | + } |
| 24 | + $allot[] = [$i,$turnedOn - $i]; |
| 25 | + } |
| 26 | + |
| 27 | + $res = []; |
| 28 | + foreach($allot as $v){ |
| 29 | + $this->minRes = []; |
| 30 | + $this->hourRes = []; |
| 31 | + |
| 32 | + $this->getHour($v[0]); |
| 33 | + $this->getMin($v[1]); |
| 34 | + |
| 35 | + $this->hourRes = array_unique($this->hourRes); |
| 36 | + $this->minRes = array_unique($this->minRes); |
| 37 | + |
| 38 | + foreach($this->hourRes as $v0){ |
| 39 | + foreach($this->minRes as $v1){ |
| 40 | + $res[] = "{$v0}:{$v1}"; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return $res; |
| 46 | + } |
| 47 | + |
| 48 | + function getHour($num,$hour = 0,$hourArr = [8,4,2,1]){ |
| 49 | + if($num == 0){ |
| 50 | + $this->hourRes[] = '0'; |
| 51 | + } |
| 52 | + |
| 53 | + foreach($hourArr as $k => $v){ |
| 54 | + $temp = $v + $hour; |
| 55 | + if($temp > 11){ |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if($num == 1){ |
| 60 | + $this->hourRes[] = $temp; |
| 61 | + }else{ |
| 62 | + unset($hourArr[$k]); |
| 63 | + $this->getHour($num - 1,$temp,$hourArr); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function getMin($num,$min = 0,$minArr = [32,16,8,4,2,1]){ |
| 69 | + if($num == 0){ |
| 70 | + $this->minRes[] = '00'; |
| 71 | + } |
| 72 | + |
| 73 | + foreach($minArr as $k => $v){ |
| 74 | + $temp = $v + $min; |
| 75 | + if($temp > 59){ |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if($num == 1){ |
| 80 | + $this->minRes[] = str_pad($temp,2,'0',STR_PAD_LEFT); |
| 81 | + }else{ |
| 82 | + unset($minArr[$k]); |
| 83 | + $this->getMin($num - 1,$temp,$minArr); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +$turnedOn = 2; |
| 90 | +$m = new L401_BinaryWatch(); |
| 91 | +print_r($m->readBinaryWatch($turnedOn)); |
0 commit comments