知耻而后勇, 日积月累, 每月一篇博客 以下内容, 记录自己刷题过程
720. 词典中最长的单词 LeetCode - 720. 词典中最长的单词
解题思路: 字符串排序 + 哈希集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Solution { function longestWord ($words ): String { usort ($words , function ($i , $j ) { if (strlen ($i ) != strlen ($j )) { return strlen ($i ) - strlen ($j ); } return $i > $j ? -1 : 1 ; }); $longest = "" ; $hashset = []; $hashset [] = "" ; foreach ($words as $word ) { if (in_array (substr ($word , 0 , strlen ($word ) - 1 ), $hashset )) { $longest = $word ; $hashset [] = $word ; } } return $longest ; } }
2043. 简易银行系统 LeetCode - 2043. 简易银行系统
解题思路: 数组模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 class Bank { private $balance ; function __construct ($balance ) { $this ->balance = $balance ; } function transfer ($account1 , $account2 , $money ) { if (!$this ->withdraw ($account1 , $money )) return false ; if (!$this ->deposit ($account2 , $money )) { $this ->deposit ($account1 , $money ); return false ; } return true ; } function deposit ($account , $money ) { if (!isset ($this ->balance[$account - 1 ])) return false ; $this ->balance[$account - 1 ] += $money ; return true ; } function withdraw ($account , $money ) { if (!isset ($this ->balance[$account - 1 ])) return false ; if ($this ->balance[$account - 1 ] < $money ) return false ; $this ->balance[$account - 1 ] -= $money ; return true ; } }
606. 根据二叉树创建字符串 LeetCode - 606. 根据二叉树创建字符串
解题思路: 递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution { function tree2str (TreeNode $root ) { if ($root == null ) return "" ; if ($root ->left == '' && $root ->right == '' ) { return (string )$root ->val; } if ($root ->right == null ) { return (string )$root ->val . '(' . $this ->tree2str ($root ->left) . ')' ; } return (string )$root ->val . "(" . $this ->tree2str ($root ->left) . ')(' . $this ->tree2str ($root ->right) . ')' ; } } class TreeNode { public $val = null ; public $left = null ; public $right = null ; function __construct ($val = 0 , $left = null , $right = null ) { $this ->val = $val ; $this ->left = $left ; $this ->right = $right ; } }
解题思路: 迭代
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 class Solution { function tree2str ($root ) { $ans = '' ; $stack = new SplStack (); $stack ->push ($root ); $vis = []; while (!$stack ->isEmpty ()) { $node = $stack ->top (); if (in_array ($node , $vis , true )) { if ($node !== $root ) { $ans .= ')' ; } $stack ->pop (); } else { $vis [] = $node ; if ($node !== $root ) { $ans .= '(' ; } $ans .= $node ->val; if ($node ->left === null && $node ->right !== null ) { $ans .= '()' ; } if ($node ->right !== null ) { $stack ->push ($node ->right); } if ($node ->left !== null ) { $stack ->push ($node ->left); } } } return (string )$ans ; } } class TreeNode { public $val = null ; public $left = null ; public $right = null ; function __construct ($val = 0 , $left = null , $right = null ) { $this ->val = $val ; $this ->left = $left ; $this ->right = $right ; } }
2039. 网络空闲的时刻 LeetCode - 2039. 网络空闲的时刻
解题思路: 广度优先搜索 我们可以将整个计算机网络视为一个无向图,服务器为图中的节点。根据图中的边对应的关系 edges 即可求出图中任意节点之间的最短距离。利用广度优先搜索求出节点 00 到其他节点的最短距离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class Solution { function networkBecomesIdle ($edges , $patience ) { $n = count ($patience ); $g = array_fill (0 , $n , []); foreach ($edges as $e ) { $x = $e [0 ]; $y = $e [1 ]; $g [$x ][] = $y ; $g [$y ][] = $x ; } $vis = array_fill (0 , $n , false ); $vis [0 ] = true ; $q = [0 => 0 ]; for ($dist = 1 ; $q !== []; $dist ++) { $tmp = $q ; $q = []; foreach ($tmp as $x ) { foreach ($g [$x ] as $v ) { if ($vis [$v ]) continue ; $vis [$v ] = true ; $q [] = $v ; $ans = max ($ans , floor (($dist *2 - 1 ) / $patience [$v ]) * $patience [$v ] + $dist *2 + 1 ); } } } return $ans ; } }
653. 两数之和 IV - 输入 BST LeetCode - 653. 两数之和 IV - 输入 BST
解题思路: 深度优先搜索 + 哈希表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class Solution { function findTarget ($root , $k ) { $array [] = "" ; return $this ->dfs ($root , $k , $array ); } function dfs ($root , $k , &$array ) { if ($root ->val === null ) return false ; if (in_array (($k - $root ->val), $array )) { return true ; } $array [] = $root ->val; return $this ->dfs ($root ->left, $k , $array ) || $this ->dfs ($root ->right, $k , $array ); } }
解题思路: 广度优先搜索 + 哈希表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 class Solution { function findTarget ($root , $k ) { $set [] = '' ; $queue = new SplQueue (); $queue ->push ($root ); while (!$queue ->isEmpty ()) { $node = $queue ->shift (); if (in_array (($k - $node ->val), $set )) { return true ; } array_push ($set , $node ->val); if ($node ->left) { $queue ->push ($node ->left); } if ($node ->right) { $queue ->push ($node ->right); } } return false ; } }
2038. 如果相邻两个颜色均相同则删除当前颜色 LeetCode - 2038. 如果相邻两个颜色均相同则删除当前颜色
解题思路: 贪心算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 class Solution { function winnerOfGame ($colors ) { $freq = [0 , 0 ]; $cur = 'C' ; $cnt = 0 ; for ($i = 0 ; $i < strlen ($colors ); $i ++) { $c = $colors [$i ]; if ($c !== $cur ) { $cur = $c ; $cnt = 1 ; } else { $cnt += 1 ; if ($cnt >= 3 ) { $freq [ord ($cur ) - ord ('A' )] += 1 ; } } } return $freq [0 ] > $freq [1 ]; } }
440. 字典序的第K小数字 LeetCode - 440. 字典序的第K小数字
什么是字典序?
简而言之,就是根据数字的前缀进行排序
解题思路: 字典树思想
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Solution { function findKthNumber ($n , $k ) { $cur = 1 ; $k --; while ($k > 0 ) { $steps = $this ->getSteps ($cur , $n ); if ($steps <= $k ) { $k -= $steps ; $cur ++; } else { $cur *= 10 ; $k --; } } return $cur ; } function getSteps ($cur , $n ) { $first = $last = $cur ; while ($first <= $n ) { $steps += min ($last , $n ) - $first + 1 ; $first *= 10 ; $last = $last * 10 + 9 ; } return $steps ; } }
661. 图片平滑器 LeetCode - 661. 图片平滑器
解题思路: 遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Solution { function imageSmoother ($img ) { $h = count ($img ); $w = count ($img [0 ]); $ret = []; for ($i = 0 ; $i < $h ; $i ++) { for ($j = 0 ; $j < $w ; $j ++) { $count = 0 ; foreach ([$i - 1 , $i , $i + 1 ] as $iv ) { foreach ([$j - 1 , $j , $j + 1 ] as $jv ) { if ($iv >= 0 && $iv < $h && $jv >= 0 && $jv < $w ) { $ret [$i ][$j ] += $img [$iv ][$jv ]; $count ++; } } } $ret [$i ][$j ] = floor ($ret [$i ][$j ]/$count ); } } return $ret ; } }
172. 阶乘后的零 LeetCode - 172. 阶乘后的零
解题思路: 质因数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { function trailingZeroes ($n ) { $num = 0 ; while ($n > 0 ) { $n = (int )($n /5 ); $num += $n ; } return $num ; } }
682. 棒球比赛 LeetCode - 682. 棒球比赛
解题思路: 链表长度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 class Solution { function removeNthFromEnd ($head , $n ) { $dummyHead = new ListNode (0 , $head ); $length = $this ->getLength ($head ); $cur = $dummyHead ; for ($i = 1 ; $i < $length - $n + 1 ; $i ++) { $cur = $cur ->next; } $cur ->next = $cur ->next->next; return $dummyHead ->next; } private function getLength ($head ) { $length = 0 ; while ($head != null ) { $head = $head ->next; $length ++; } return $length ; } }
解题思路: 栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 function removeNthFromEnd ($head , $n ) { $dummyHead = new ListNode (0 , $head ); $cur = $dummyHead ; $stack = new SplStack (); while ($cur != null ) { $stack ->push ($cur ); $cur = $cur ->next; } for ($i = 0 ; $i < $n ; $i ++) { $stack ->pop (); } $pre = $stack ->top (); $pre ->next = $pre ->next->next; return $dummyHead ->next; }
解题思路: 双指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function removeNthFromEnd ($head , $n ) { $dummyHead = new ListNode (0 , $head ); $first = $head ; $second = $dummyHead ; for ($i = 0 ; $i < $n ; $i ++) { $first = $first ->next; } while ($first != null ) { $first = $first ->next; $second = $second ->next; } $second ->next = $second ->next->next; return $dummyHead ->next; }
2028. 找出缺失的观测数据 LeetCode - 2028. 找出缺失的观测数据
解题思路: 模拟构造
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { function missingRolls ($rolls , $mean , $n ) { $missSum = ($mean * ($n + count ($rolls ))) - array_sum ($rolls ); if ($missSum < $n || $missSum > $n *6 ) return []; $avg = (int )($missSum / $n ); $less = $missSum % $n ; $ans = array_fill (0 , $n , $avg ); for ($i = 0 ; $i < $n ; $i ++) { $ans [$i ] = $avg + ($i < $less ? 1 : 0 ); } return $ans ; } }
693. 交替位二进制数 LeetCode - 693. 交替位二进制数
解题思路: 位运算
1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution { function hasAlternatingBits ($n ) { $tmp = $n ^ ($n >> 1 ); return ($tmp & ($tmp + 1 )) == 0 ; } }
2024. 考试的最大困扰度 LeetCode - 2024. 考试的最大困扰度
解题思路: 滑动窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution { function maxConsecutiveAnswers ($answerKey , $k ) { return max ($this ->maxConsecutiveChar ($answerKey , $k , 'T' ), $this ->maxConsecutiveChar ($answerKey , $k , 'F' )); } function maxConsecutiveChar ($answerKey , $k , $byte ) { $ans = $left = $sum = 0 ; $n = strlen ($answerKey ); for ($i = 0 ; $i < $n ; $i ++) { if ($answerKey [$i ] != $byte ) $sum ++; while ($sum > $k ) { if ($answerKey [$left ] != $byte ) $sum --; $left ++; } $ans = max ($ans , $i - $left + 1 ); } return $ans ; } }
1606. 找到处理最多请求的服务器 LeetCode - 1606. 找到处理最多请求的服务器
解题思路:
728. 自除数 LeetCode - 728. 自除数
解题思路: 模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { function selfDividingNumbers ($left , $right ) { $array = []; for ($num = $left ; $left <= $right ; $num = ++$left ) { while ($num ) { $mod = $num % 10 ; if ($mod == 0 || $left % $mod != 0 ) { continue 2 ; } $num = (int )($num / 10 ); } $array [] = $left ; } return $array ; } }