├── 03th数组、链表、跳表 ├── 1.两数之和.php ├── 11.盛最多水的容器.php ├── 15.三数之和.php ├── 189.旋转数组.php ├── 232.用栈实现队列.php ├── 26.删除排序数组中的重复项.php ├── 283.移动零.php ├── 70.爬楼梯.php └── README.md ├── 04th栈、队列、优先队列、双端队列 ├── 155.最小栈(fail).php ├── 155.最小栈.php ├── 20.有效的括号.php ├── 239.滑动窗口最大值(timeout).php ├── 42.接雨水.php ├── 641.设计循环双端队列.php └── README.md ├── 05th哈希表、映射、集合 ├── 242.有效的字母异位词.php ├── 242.有效的字母异位词_加速.php ├── 49.字母异位词分组.php └── README.md ├── 06th树、二叉树、二叉搜索树 ├── 06th最小的k个数.php ├── 06th最小的k个数_快排.php ├── 144.二叉树的前序遍历_迭代.php ├── 239.滑动窗口最大值.php ├── 264.丑数-ii.php ├── 347.前-k-个高频元素.php ├── 589.n叉树的前序遍历.php ├── 590.n叉树的后序遍历.php ├── 590.n叉树的后序遍历_迭代.php ├── 94.二叉树的中序遍历.php ├── 94.二叉树的中序遍历_迭代.php └── README.md ├── 07th泛型递归、树的递归 ├── 104.二叉树的最大深度.php ├── 105.从前序与中序遍历序列构造二叉树.php ├── 105.从前序与中序遍历序列构造二叉树_加速.php ├── 111.二叉树的最小深度.php ├── 22.括号生成.php ├── 226.翻转二叉树.php ├── 236.二叉树的最近公共祖先.php ├── 46.全排列.php ├── 47.全排列-ii.php ├── 47.全排列-ii_加速.php ├── 70.爬楼梯_递归.php ├── 77.组合.php ├── 77.组合_加速.php ├── 98.验证二叉搜索树.php └── README.md ├── 08th分治、回溯 ├── 17.电话号码的字母组合.php ├── 50.pow-x-n.php ├── 51.n皇后.php ├── 78.子集_回溯.php ├── 78.子集_递归.php └── README.md ├── 09th深度优先搜索和广度优先搜索 ├── 102.二叉树的层序遍历_BFS.php ├── 127.单词接龙_超时.php ├── 200.岛屿数量_DFS.php ├── 22.括号生成_BFS.php ├── 22.括号生成_DFS.php ├── 433.最小基因变化_BFS.php ├── 529.扫雷游戏_DFS.php └── README.md ├── 10th贪心算法 ├── 122.买卖股票的最佳时机-ii.php ├── 455.分发饼干.php ├── 55.跳跃游戏_贪心1.php ├── 55.跳跃游戏_贪心2.php └── README.md ├── 11th二分查找 ├── 33.搜索旋转排序数组.php ├── 367.有效的完全平方数.php ├── 69.x-的平方根.php ├── 74.搜索二维矩阵.php └── README.md ├── 12th动态规划 ├── 1143.最长公共子序列.php ├── 120.三角形最小路径和.php ├── 120.三角形最小路径和_优化内存.php ├── 121.买卖股票的最佳时机.php ├── 152.乘积最大子数组.php ├── 198.打家劫舍_DP.php ├── 198.打家劫舍_优化内存.php ├── 221.最大正方形.php ├── 312.戳气球_DP.php ├── 32.最长有效括号.php ├── 322.零钱兑换_自底向上DP.php ├── 410.分割数组的最大值.php ├── 53.最大子序和.php ├── 53.最大子序和_DP.php ├── 62.不同路径.php ├── 62.不同路径_优化内存.php ├── 621.任务调度器.php ├── 63.不同路径-ii.php ├── 64.最小路径和.php ├── 647.回文子串.php ├── 70.爬楼梯.php ├── 72.编辑距离.php ├── 91.解码方法.php └── README.md ├── 13th字典树和并查集 ├── 130.被围绕的区域.php ├── 200.岛屿数量.php ├── 547.朋友圈_BFS.php ├── 547.朋友圈_DFS.php ├── 547.朋友圈_UnionFind.php └── README.md ├── 14th高级搜索 ├── 22.括号生成.php ├── 433.最小基因变化.php ├── 51.n皇后.php ├── 70.爬楼梯.php └── README.md ├── 15th红黑树和AVL树 └── README.md ├── 16th位运算 ├── 190.颠倒二进制位.php ├── 191.位-1-的个数.php ├── 231.2-的幂.php └── README.md ├── 17th布隆过滤器和 LRU 缓存 ├── 146.lru缓存机制.php └── README.md ├── 18th排序算法 ├── 1122.数组的相对排序.php ├── 242.有效的字母异位词.php ├── 493.翻转对.php ├── 56.合并区间.php └── README.md ├── 19th高级动态规划 ├── 115.不同的子序列.php ├── 300.最长上升子序列.php ├── 32.最长有效括号.php ├── 818.赛车_超时.php └── README.md ├── 20th字符串算法 ├── 151.翻转字符串里的单词.php ├── 205.同构字符串.php ├── 387.字符串中的第一个唯一字符.php ├── 438.找到字符串中所有字母异位词.php ├── 541.反转字符串-ii.php ├── 557.反转字符串中的单词-iii.php ├── 8.字符串转换整数-atoi.php ├── 917.仅仅反转字母.php └── README.md ├── 21剑指offer系列 └── README.md ├── 22程序员面试经典 └── README.md ├── 23刷题路线 ├── 128.最长连续序列.php ├── 128.最长连续序列_加速.php ├── README.md ├── [基础]1.两数之和.php ├── [基础]146.lru缓存机制.php ├── [基础]20.有效的括号.php ├── [基础]208.实现-trie-前缀树.php ├── [基础]211.添加与搜索单词-数据结构设计.php ├── [基础]212.单词搜索-ii.php ├── [基础]212.单词搜索-ii_超时.php └── [基础]394.字符串解码.php ├── README.md ├── 每日1题202007 ├── README.md ├── [0713]350.两个数组的交集-ii.php ├── [0714]120.三角形最小路径和.php ├── [0715]96.不同的二叉搜索树.php ├── [0717]35.搜索插入位置.php ├── [0718]97.交错字符串.php ├── [0719]312.戳气球_动态规划DP.php ├── [0719]312.戳气球_回溯法(超时).php ├── [0720]167.两数之和-ii-输入有序数组.php ├── [0721]95.不同的二叉搜索树-ii_递归.php ├── [0722]剑指 Offer 11. 旋转数组的最小数字.php ├── [0723]64.最小路径和.php ├── [0724]1025.除数博弈.php ├── [0725]410.分割数组的最大值.php ├── [0726]329.矩阵中的最长递增路径.php ├── [0727]392.判断子序列.php ├── [0728]104.二叉树的最大深度.php ├── [0729]LCP 13. 寻宝.php ├── [0730]343.整数拆分.php └── [0731]面试题 08.03. 魔术索引.php ├── 每日1题202008 ├── README.md ├── [0801]632.最小区间_未通过.php ├── [0802]114.二叉树展开为链表.php ├── [0803]415.字符串相加.php ├── [0804]207.课程表.php ├── [0805]337.打家劫舍-iii.php ├── [0806]336.回文对.php ├── [0807]100.相同的树.php ├── [0808]99.恢复二叉搜索树.php ├── [0809]93.复原ip地址.php ├── [0810]696.计数二进制子串.php ├── [0811]130.被围绕的区域.php ├── [0812]133.克隆图.php ├── [0813]43.字符串相乘_优化版.php ├── [0813]43.字符串相乘_普通版.php ├── [0814]20.有效的括号.php ├── [0815]546.移除盒子.php ├── [0816]733.图像渲染.php ├── [0817]110.平衡二叉树.php ├── [0818]109.有序链表转换二叉搜索树.php ├── [0819]647.回文子串.php ├── [0820]529.扫雷游戏.php ├── [0821]111.二叉树的最小深度.php ├── [0822]679.24-点游戏.php ├── [0823]201.数字范围按位与.php ├── [0824]459.重复的子字符串.php ├── [0825]491.递增子序列.php ├── [0826]17.电话号码的字母组合.php ├── [0827]332.重新安排行程.php ├── [0828]657.机器人能否返回原点.php ├── [0829]214.最短回文串.php ├── [0830]557.反转字符串中的单词-iii.php └── [0831]841.钥匙和房间.php ├── 每日1题202009 ├── README.md ├── [0901]486.预测赢家_记忆化递归.php ├── [0901]486.预测赢家_递归.php ├── [0904]257.二叉树的所有路径.php ├── [0906]107.二叉树的层次遍历-ii.php ├── [0907]347.前-k-个高频元素.php ├── [0908]77.组合.php ├── [0909]39.组合总和.php ├── [0910]40.组合总和-ii.php ├── [0914]94.二叉树的中序遍历.php ├── [0914_1]144.二叉树的前序遍历.php ├── [0914_2]145.二叉树的后序遍历.php ├── [0915]37.解数独.php ├── [0916]226.翻转二叉树.php ├── [0917]685.冗余连接-ii_并查集.php ├── [0917]685.冗余连接-ii_非并查集.php ├── [0918]47.全排列-ii.php ├── [0919]404.左叶子之和.php ├── [0920]78.子集.php ├── [0921]538.把二叉搜索树转换为累加树.php ├── [0922]968.监控二叉树.php ├── [0923]617.合并二叉树.php ├── [0924]501.二叉搜索树中的众数.php ├── [0924]501.二叉搜索树中的众数_中序遍历.php ├── [0925]106.从中序与后序遍历序列构造二叉树.php ├── [0927]235.二叉搜索树的最近公共祖先.php ├── [0928]117.填充每个节点的下一个右侧节点指针-ii.php ├── [0928]117.填充每个节点的下一个右侧节点指针-ii_加速.php └── [0930]701.二叉搜索树中的插入操作.php ├── 每日1题202010 ├── README.md ├── [1003]1.两数之和.php ├── [1007]75.颜色分类_单向遍历.php ├── [1007]75.颜色分类_双指针.php ├── [1008]344.反转字符串_双指针.php ├── [1009]141.环形链表_哈希表.php ├── [1009]141.环形链表_快慢指针.php ├── [1010]142.环形链表-ii.php ├── [1014]1002.查找常用字符.php ├── [1016]977.有序数组的平方_双指针.php ├── [1016]977.有序数组的平方_常规解法.php ├── [1017]52.n皇后-ii_回溯剪枝.php ├── [1018]19.删除链表的倒数第n个节点.php ├── [1018]19.删除链表的倒数第n个节点_双指针.php ├── [1019]844.比较含退格的字符串.php ├── [1020]143.重排链表_线性表存储.php ├── [1020]143.重排链表_递归.php ├── [1021]925.长按键入.php ├── [1022]763.划分字母区间.php ├── [1022]763.划分字母区间_暴力解法.php ├── [1023]234.回文链表_数组存储.php ├── [1028]1207.独一无二的出现次数.php ├── [1029]129.求根到叶子节点数字之和.php └── [1030]463.岛屿的周长.php └── 每日1题202011 ├── README.md ├── [1102]349.两个数组的交集_暴力解法.php └── [1103]941.有效的山脉数组.php /03th数组、链表、跳表/1.两数之和.php: -------------------------------------------------------------------------------- 1 | $v) { 21 | 22 | if(isset($result[$target-$v]) && $nums[$result[$target-$v]] + $v === $target){ 23 | return [$result[$target-$v], $k]; 24 | } 25 | 26 | $result[$v] = $k; 27 | } 28 | } 29 | } 30 | // @lc code=end 31 | 32 | -------------------------------------------------------------------------------- /03th数组、链表、跳表/11.盛最多水的容器.php: -------------------------------------------------------------------------------- 1 | $height[$j] ? $j-- : $i++; 28 | } 29 | 30 | return $max; 31 | } 32 | } 33 | // @lc code=end 34 | 35 | -------------------------------------------------------------------------------- /03th数组、链表、跳表/15.三数之和.php: -------------------------------------------------------------------------------- 1 | 0 && $nums[$i] == $nums[$i-1]) continue; 29 | 30 | // 头尾指针 31 | $head = $i+1; 32 | $tail = count($nums)-1; 33 | 34 | while ($head < $tail) { 35 | $sum = -($nums[$head] + $nums[$tail]); 36 | 37 | if($sum == $nums[$i]){ 38 | $result[] = [$nums[$i], $nums[$head], $nums[$tail]]; 39 | 40 | while ($head < $tail && $nums[$head] == $nums[$head+1]) { 41 | $head++; 42 | } 43 | 44 | while ($head < $tail && $nums[$tail] == $nums[$tail-1]) { 45 | $tail--; 46 | } 47 | } 48 | 49 | $sum >= $nums[$i] ? $head++ : $tail--; 50 | } 51 | } 52 | 53 | return $result; 54 | } 55 | } 56 | // @lc code=end -------------------------------------------------------------------------------- /03th数组、链表、跳表/189.旋转数组.php: -------------------------------------------------------------------------------- 1 | reverse($nums, 0, count($nums)-1); 20 | $this->reverse($nums, 0, $k-1); 21 | $this->reverse($nums, $k, count($nums)-1); 22 | 23 | return $nums; 24 | } 25 | 26 | function reverse(&$nums, $start, $end) { 27 | 28 | while ($start < $end) { 29 | $temp = $nums[$start]; 30 | $nums[$start] = $nums[$end]; 31 | $nums[$end] = $temp; 32 | $start++;$end--; 33 | } 34 | } 35 | } 36 | // @lc code=end 37 | 38 | -------------------------------------------------------------------------------- /03th数组、链表、跳表/26.删除排序数组中的重复项.php: -------------------------------------------------------------------------------- 1 | data,$x); 24 | } 25 | 26 | /** 27 | * @return NULL 28 | */ 29 | function pop() { 30 | if(empty($this->data)){ 31 | return null; 32 | } 33 | return array_pop($this->data); 34 | } 35 | 36 | /** 37 | * @return Integer 38 | */ 39 | function top() { 40 | if(empty($this->data)){ 41 | return null; 42 | } 43 | 44 | $temp = end($this->data); 45 | reset($this->data); 46 | 47 | return $temp; 48 | } 49 | 50 | /** 51 | * @return Integer 52 | */ 53 | function getMin() { 54 | if(empty($this->data)){ 55 | return null; 56 | } 57 | return min($this->data); 58 | } 59 | } 60 | 61 | /** 62 | * Your MinStack object will be instantiated and called as such: 63 | * $obj = MinStack(); 64 | * $obj->push($x); 65 | * $obj->pop(); 66 | * $ret_3 = $obj->top(); 67 | * $ret_4 = $obj->getMin(); 68 | */ 69 | // @lc code=end 70 | 71 | -------------------------------------------------------------------------------- /04th栈、队列、优先队列、双端队列/20.有效的括号.php: -------------------------------------------------------------------------------- 1 | "}","("=>")","["=>"]","?"=>"?"]; 18 | $left_arr = ["{","(","["]; 19 | $stack = ["?"]; 20 | 21 | for ($i=0; $i < strlen($s); $i++) { 22 | 23 | if(in_array($s[$i], $left_arr)){ 24 | $stack[] = $s[$i]; 25 | }elseif($arr[array_pop($stack)] != $s[$i]){ 26 | return false; 27 | } 28 | } 29 | 30 | return count($stack) == 1; 31 | } 32 | } 33 | // @lc code=end 34 | 35 | -------------------------------------------------------------------------------- /04th栈、队列、优先队列、双端队列/239.滑动窗口最大值(timeout).php: -------------------------------------------------------------------------------- 1 | $left_max){ 28 | $left_max = $height[$i]; 29 | } 30 | 31 | if($height[$n-$i-1] > $right_max){ 32 | $right_max = $height[$n-$i-1]; 33 | } 34 | 35 | $S1 += $left_max; 36 | $S2 += $right_max; 37 | } 38 | 39 | return $S1 + $S2 - $left_max*$n - array_sum($height)*1; 40 | } 41 | } 42 | // @lc code=end 43 | 44 | -------------------------------------------------------------------------------- /04th栈、队列、优先队列、双端队列/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /05th哈希表、映射、集合/242.有效的字母异位词.php: -------------------------------------------------------------------------------- 1 | 0){ 40 | return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | } 47 | 48 | // Accepted 49 | // 34/34 cases passed (16 ms) 50 | // Your runtime beats 63.69 % of php submissions 51 | // Your memory usage beats 100 % of php submissions (14.9 MB) 52 | // @lc code=end 53 | 54 | -------------------------------------------------------------------------------- /05th哈希表、映射、集合/242.有效的字母异位词_加速.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer[] 23 | */ 24 | function preorderTraversal($root) { 25 | 26 | $white = 0; 27 | $gray = 1; 28 | $stack[] = [$white, $root]; 29 | $res = []; 30 | 31 | while (!empty($stack)) { 32 | $cur = array_pop($stack); 33 | 34 | if($cur[1] == null) continue; 35 | 36 | if($cur[0] == $white){ 37 | $stack[] = [$white, $cur[1]->right]; 38 | $stack[] = [$white, $cur[1]->left]; 39 | $stack[] = [$gray, $cur[1]]; 40 | }else{ 41 | $res[] = $cur[1]->val; 42 | } 43 | } 44 | 45 | return $res; 46 | } 47 | } 48 | 49 | // Accepted 50 | // 68/68 cases passed (4 ms) 51 | // Your runtime beats 95.18 % of php submissions 52 | // Your memory usage beats 100 % of php submissions (14.6 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/264.丑数-ii.php: -------------------------------------------------------------------------------- 1 | $val) { 28 | 29 | $new_res[] = $key; 30 | if(++$i == $k) break; 31 | } 32 | 33 | return $new_res; 34 | } 35 | } 36 | // @lc code=end 37 | 38 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/589.n叉树的前序遍历.php: -------------------------------------------------------------------------------- 1 | val = $val; 16 | * $this->children = array(); 17 | * } 18 | * } 19 | */ 20 | 21 | class Solution { 22 | /** 23 | * @param Node $root 24 | * @return integer[] 25 | */ 26 | function preorder($root) { 27 | 28 | $res = [$root->val]; 29 | $this->backorder($root, $res); 30 | return $res; 31 | } 32 | 33 | function backorder($root, &$res) { 34 | 35 | if($root == null) return; 36 | 37 | foreach ($root->children as $child) { 38 | $res[] = $child->val; 39 | $this->backorder($child, $res); 40 | } 41 | } 42 | } 43 | 44 | // Accepted 45 | // 37/37 cases passed (16 ms) 46 | // Your runtime beats 73.61 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (18.7 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/590.n叉树的后序遍历.php: -------------------------------------------------------------------------------- 1 | val = $val; 16 | * $this->children = array(); 17 | * } 18 | * } 19 | */ 20 | 21 | class Solution { 22 | /** 23 | * @param Node $root 24 | * @return integer[] 25 | */ 26 | function postorder($root) { 27 | 28 | $res = []; 29 | $this->backorder($root, $res); 30 | $res[] = $root->val; 31 | return $res; 32 | } 33 | 34 | function backorder($root, &$res) { 35 | 36 | if($root == null) return; 37 | 38 | foreach ($root->children as $child) { 39 | $this->backorder($child, $res); 40 | $res[] = $child->val; 41 | } 42 | } 43 | } 44 | 45 | // Accepted 46 | // 37/37 cases passed (16 ms) 47 | // Your runtime beats 71.43 % of php submissions 48 | // Your memory usage beats 100 % of php submissions (18.6 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/590.n叉树的后序遍历_迭代.php: -------------------------------------------------------------------------------- 1 | val = $val; 16 | * $this->children = array(); 17 | * } 18 | * } 19 | */ 20 | 21 | class Solution { 22 | /** 23 | * @param Node $root 24 | * @return integer[] 25 | */ 26 | function postorder($root) { 27 | 28 | // 利用后序遍历与前序遍历结果类似,顺序入栈子节点v1 v2 v3,出栈的时候v3 v2 v1 29 | // 最后结果反转还是保持 v1 v2 v3 30 | 31 | $res = []; 32 | $stack = [$root]; 33 | 34 | while (!empty($stack)) { 35 | $root = array_pop($stack); 36 | 37 | if($root != null){ 38 | $res[] = $root->val; 39 | } 40 | 41 | foreach ($root->children as $child) { 42 | $stack[] = $child; 43 | } 44 | } 45 | 46 | return array_reverse($res); 47 | } 48 | } 49 | 50 | // Accepted 51 | // 37/37 cases passed (12 ms) 52 | // Your runtime beats 92.86 % of php submissions 53 | // Your memory usage beats 100 % of php submissions (18.6 MB) 54 | // @lc code=end 55 | 56 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/94.二叉树的中序遍历.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer[] 23 | */ 24 | function inorderTraversal($root) { 25 | 26 | $result = []; 27 | $this->inorder($root, $result); 28 | 29 | return $result; 30 | 31 | } 32 | 33 | function inorder($root, &$result){ 34 | 35 | if($root->left != null){ 36 | $this->inorder($root->left, $result); 37 | } 38 | 39 | $result[] = $root->val; 40 | 41 | if($root->right != null){ 42 | $this->inorder($root->right, $result); 43 | } 44 | } 45 | } 46 | 47 | // Accepted 48 | // 68/68 cases passed (8 ms) 49 | // Your runtime beats 57.14 % of php submissions 50 | // Your memory usage beats 100 % of php submissions (14.8 MB) 51 | // @lc code=end 52 | 53 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/94.二叉树的中序遍历_迭代.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer[] 23 | */ 24 | function inorderTraversal($root) { 25 | 26 | $white = 0; 27 | $gray = 1; 28 | $stack[] = [$white, $root]; 29 | $res = []; 30 | 31 | while (!empty($stack)) { 32 | $cur = array_pop($stack); 33 | 34 | if($cur[1] == null) continue; 35 | 36 | if($cur[0] == 0){ 37 | $stack[] = [$white, $cur[1]->right]; 38 | $stack[] = [$gray, $cur[1]]; 39 | $stack[] = [$white, $cur[1]->left]; 40 | }else{ 41 | $res[] = $cur[1]->val; 42 | } 43 | } 44 | 45 | return $res; 46 | } 47 | } 48 | 49 | // Accepted 50 | // 68/68 cases passed (4 ms) 51 | // Your runtime beats 90.59 % of php submissions 52 | // Your memory usage beats 100 % of php submissions (14.9 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /06th树、二叉树、二叉搜索树/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /07th泛型递归、树的递归/104.二叉树的最大深度.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer 23 | */ 24 | function maxDepth($root) { 25 | 26 | if($root == null) { 27 | return 0; 28 | } 29 | 30 | $left = $this->maxDepth($root->left); 31 | $right = $this->maxDepth($root->right); 32 | 33 | return max($left, $right) + 1; 34 | } 35 | } 36 | 37 | // Accepted 38 | // 39/39 cases passed (16 ms) 39 | // Your runtime beats 39.45 % of php submissions 40 | // Your memory usage beats 100 % of php submissions (16.9 MB) 41 | // @lc code=end 42 | 43 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/111.二叉树的最小深度.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer 23 | */ 24 | function minDepth($root) { 25 | 26 | if($root == null) return 0; 27 | 28 | if($root->left == null && $root->right == null) return 1; 29 | 30 | $min_depth = PHP_INT_MAX; 31 | if($root->left != null) { 32 | $min_depth = min($this->minDepth($root->left), $min_depth); 33 | } 34 | 35 | if($root->right != null) { 36 | $min_depth = min($this->minDepth($root->right), $min_depth); 37 | } 38 | 39 | return $min_depth + 1; 40 | } 41 | } 42 | 43 | // Accepted 44 | // 41/41 cases passed (12 ms) 45 | // Your runtime beats 84.21 % of php submissions 46 | // Your memory usage beats 100 % of php submissions (17.2 MB) 47 | // @lc code=end 48 | 49 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/22.括号生成.php: -------------------------------------------------------------------------------- 1 | generate(0, 0, $n, "", $res); 20 | 21 | return $res; 22 | } 23 | 24 | // 递归生成括号 25 | function generate($left, $right, $n, $s, &$res) { 26 | 27 | // 每次终止条件 28 | if($left == $n && $right == $n) { 29 | $res[] = $s; 30 | } 31 | 32 | // 左括号还没用完 33 | if($left < $n) { 34 | $this->generate($left+1, $right, $n, $s."(", $res); 35 | } 36 | 37 | // 右括号小于左口号数量时,+1 38 | if($right < $left) { 39 | $this->generate($left, $right+1, $n, $s.")", $res); 40 | } 41 | } 42 | } 43 | 44 | // Accepted 45 | // 8/8 cases passed (8 ms) 46 | // Your runtime beats 79.7 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (15.7 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/226.翻转二叉树.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return TreeNode 23 | */ 24 | function invertTree($root) { 25 | 26 | if($root == null) return null; 27 | 28 | $right = $this->invertTree($root->right); 29 | $left = $this->invertTree($root->left); 30 | 31 | $root->right = $left; 32 | $root->left = $right; 33 | 34 | return $root; 35 | } 36 | } 37 | 38 | // Accepted 39 | // 68/68 cases passed (4 ms) 40 | // Your runtime beats 91.06 % of php submissions 41 | // Your memory usage beats 100 % of php submissions (15 MB) 42 | // @lc code=end 43 | 44 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/236.二叉树的最近公共祖先.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | /** 21 | * @param TreeNode $root 22 | * @param TreeNode $p 23 | * @param TreeNode $q 24 | * @return TreeNode 25 | */ 26 | function lowestCommonAncestor($root, $p, $q) { 27 | 28 | if($root == null || $root==$p || $root==$q) return $root; 29 | 30 | $left = $this->lowestCommonAncestor($root->left, $p, $q); 31 | $right = $this->lowestCommonAncestor($root->right, $p, $q); 32 | 33 | // 左右非空,则返回当前节点 34 | if($left && $right) return $root; 35 | 36 | // 从是否在一颗子树角度想就很好理解,在不同子树时就返回root就好,当p为q的祖先节点时,搜索一侧子树只能返回p, 37 | // 这时候搜另一边是搜不到q的,但节点又一定在树中,所以一定是p是q的祖先的情况,直接返回p即为答案 38 | return $left ? $left : $right; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 31/31 cases passed (32 ms) 44 | // Your runtime beats 59.68 % of php submissions 45 | // Your memory usage beats 100 % of php submissions (21.4 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/46.全排列.php: -------------------------------------------------------------------------------- 1 | backtrack($res, $nums, $visited, $tmp); 22 | return $res; 23 | } 24 | 25 | function backtrack(&$res, $nums, &$visited, &$tmp) { 26 | 27 | if(count($tmp) == count($nums)) { 28 | $res[] = $tmp; 29 | return true; 30 | } 31 | 32 | for ($i=0; $i < count($nums); $i++) { 33 | // 不能重复 34 | if($visited[$i] == 1) continue; 35 | $visited[$i] = 1; 36 | $tmp[] = $nums[$i]; 37 | $this->backtrack($res, $nums, $visited, $tmp); 38 | $visited[$i] = 0; 39 | array_pop($tmp); 40 | } 41 | } 42 | } 43 | 44 | // Accepted 45 | // 25/25 cases passed (4 ms) 46 | // Your runtime beats 98.8 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (15.4 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/47.全排列-ii.php: -------------------------------------------------------------------------------- 1 | in = []; 22 | 23 | $this->backtrack($res, $nums, $visited, $tmp); 24 | return $res; 25 | } 26 | 27 | function backtrack(&$res, $nums, &$visited, &$tmp) { 28 | 29 | if(count($tmp) == count($nums)) { 30 | 31 | $tmp_in = implode('-', $tmp); 32 | if(!in_array($tmp_in, $this->in)){ 33 | $res[] = $tmp; 34 | $this->in[] = $tmp_in; 35 | } 36 | return true; 37 | } 38 | 39 | for ($i=0; $i < count($nums); $i++) { 40 | // 不能重复 41 | if($visited[$i] == 1) continue; 42 | $visited[$i] = 1; 43 | $tmp[] = $nums[$i]; 44 | $this->backtrack($res, $nums, $visited, $tmp); 45 | $visited[$i] = 0; 46 | array_pop($tmp); 47 | } 48 | } 49 | } 50 | 51 | // 直接模仿了46全排列的解法,低效率的剪枝 52 | // Accepted 53 | // 30/30 cases passed (1040 ms) 54 | // Your runtime beats 5.08 % of php submissions 55 | // Your memory usage beats 100 % of php submissions (15.2 MB) 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/47.全排列-ii_加速.php: -------------------------------------------------------------------------------- 1 | backtrack($res, $nums, $visited, $tmp); 23 | return $res; 24 | } 25 | 26 | function backtrack(&$res, $nums, &$visited, &$tmp) { 27 | 28 | if(count($tmp) == count($nums)) { 29 | $res[] = $tmp; 30 | return true; 31 | } 32 | 33 | for ($i=0; $i < count($nums); $i++) { 34 | // 不能重复 35 | if($visited[$i] == 1) continue; 36 | 37 | // 剪枝条件:$i > 0 是为了保证 $nums[$i - 1] 有意义 38 | // 写 !$visited[$i - 1] 是因为 $nums[$i - 1] 在深度优先遍历的过程中刚刚被撤销选择 39 | if ($i > 0 && $nums[$i] == $nums[$i - 1] && !$visited[$i - 1]) continue; 40 | 41 | $visited[$i] = 1; 42 | $tmp[] = $nums[$i]; 43 | 44 | $this->backtrack($res, $nums, $visited, $tmp); 45 | 46 | $visited[$i] = 0; 47 | array_pop($tmp); 48 | } 49 | } 50 | } 51 | 52 | // Accepted 53 | // 30/30 cases passed (28 ms) 54 | // Your runtime beats 50.85 % of php submissions 55 | // Your memory usage beats 100 % of php submissions (15.5 MB) 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/70.爬楼梯_递归.php: -------------------------------------------------------------------------------- 1 | climbStairs($n-1) + $this->climbStairs($n-2); 24 | } 25 | } 26 | 27 | // Time Limit Exceeded 超时 28 | // 21/45 cases passed (N/A) 29 | // @lc code=end 30 | 31 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/77.组合.php: -------------------------------------------------------------------------------- 1 | n = $n; 22 | $this->k = $k; 23 | $res = []; 24 | $tmp = []; 25 | $this->helper(1, $tmp, $res); 26 | return $res; 27 | } 28 | 29 | function helper($start, &$tmp, &$res) { 30 | 31 | if(count($tmp) == $this->k){ 32 | $res[] = $tmp; 33 | return true; 34 | } 35 | 36 | // 从 start 到 n 37 | for ($i = $start; $i <= $this->n; $i++) { 38 | $tmp[] = $i; 39 | $this->helper($i + 1, $tmp, $res); 40 | array_pop($tmp); 41 | } 42 | } 43 | } 44 | 45 | // Accepted 46 | // 27/27 cases passed (144 ms) 47 | // Your runtime beats 50.98 % of php submissions 48 | // Your memory usage beats 100 % of php submissions (19.5 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/77.组合_加速.php: -------------------------------------------------------------------------------- 1 | n = $n; 22 | $this->k = $k; 23 | $res = []; 24 | $tmp = []; 25 | $this->helper(1, $tmp, $res); 26 | return $res; 27 | } 28 | 29 | function helper($start, &$tmp, &$res) { 30 | 31 | if(count($tmp) == $this->k){ 32 | $res[] = $tmp; 33 | return true; 34 | } 35 | 36 | for ($i=$start; $i <= ($this->n - ($this->k - count($tmp)) + 1); $i++) { 37 | $tmp[] = $i; 38 | $this->helper($i + 1, $tmp, $res); 39 | array_pop($tmp); 40 | } 41 | } 42 | } 43 | 44 | // Accepted 45 | // 27/27 cases passed (32 ms) 46 | // Your runtime beats 80.39 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (19.4 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/98.验证二叉搜索树.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Boolean 23 | */ 24 | function isValidBST($root) { 25 | 26 | if($root == null) { 27 | return true; 28 | } 29 | 30 | // 初始标志 31 | $flag = true; 32 | // 初始父节点值 33 | $last = PHP_INT_MIN; 34 | return $this->helper($root, $flag, $last); 35 | } 36 | 37 | function helper($root, &$flag, &$last) { 38 | 39 | if($flag && $root->left) { 40 | $this->helper($root->left, $flag, $last); 41 | } 42 | 43 | // 遍历完左子树,得到上一个节点 44 | if($root->val <= $last){ 45 | $flag = false; 46 | } 47 | 48 | // 当前符合条件,更新last 49 | $last = $root->val; 50 | 51 | // 遍历右子树是否符合条件 52 | if($flag && $root->right) { 53 | $this->helper($root->right, $flag, $last); 54 | } 55 | 56 | return $flag; 57 | } 58 | } 59 | 60 | // Accepted 61 | // 75/75 cases passed (16 ms) 62 | // Your runtime beats 65.8 % of php submissions 63 | // Your memory usage beats 100 % of php submissions (17.7 MB) 64 | // @lc code=end 65 | 66 | -------------------------------------------------------------------------------- /07th泛型递归、树的递归/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /08th分治、回溯/17.电话号码的字母组合.php: -------------------------------------------------------------------------------- 1 | 'abc', 23 | '3' => 'def', 24 | '4' => 'ghi', 25 | '5' => 'jkl', 26 | '6' => 'mno', 27 | '7' => 'pqrs', 28 | '8' => 'tuv', 29 | '9' => 'wxyz' 30 | ]; 31 | 32 | $res = []; 33 | $this->helper("", $digits, 0, $res, $map); 34 | 35 | return $res; 36 | } 37 | 38 | function helper($s, $digits, $i, &$res, $map) { 39 | 40 | if(strlen($s) == strlen($digits)) { 41 | $res[] = $s; 42 | return true; 43 | } 44 | 45 | $letters = $map[$digits[$i]]; 46 | for ($j=0; $j < strlen($letters); $j++) { 47 | 48 | $this->helper($s.$letters[$j], $digits, $i+1, $res, $map); 49 | } 50 | } 51 | } 52 | 53 | // Accepted 54 | // 25/25 cases passed (4 ms) 55 | // Your runtime beats 91.45 % of php submissions 56 | // Your memory usage beats 100 % of php submissions (14.8 MB) 57 | // @lc code=end 58 | 59 | -------------------------------------------------------------------------------- /08th分治、回溯/50.pow-x-n.php: -------------------------------------------------------------------------------- 1 | fastPow($x, $n); 26 | } 27 | 28 | function fastPow($x, $n) { 29 | 30 | if($n == 0) { 31 | return 1.0; 32 | }elseif($n == 1){ 33 | return $x; 34 | } 35 | 36 | $half = $this->fastPow($x, $n/2); 37 | 38 | return $n%2 == 0 ? $half * $half : $half * $half * $x; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 304/304 cases passed (16 ms) 44 | // Your runtime beats 25.65 % of php submissions 45 | // Your memory usage beats 100 % of php submissions (15.4 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /08th分治、回溯/78.子集_回溯.php: -------------------------------------------------------------------------------- 1 | backtrack(0, $i, [], $nums, $res); 22 | } 23 | 24 | return $res; 25 | } 26 | 27 | function backtrack($first, $k, $cur, $nums, &$res) { 28 | 29 | if(count($cur) == $k) { 30 | $res[] = $cur; 31 | return; 32 | } 33 | 34 | for ($i=$first; $i < count($nums); $i++) { 35 | 36 | $cur[] = $nums[$i]; 37 | 38 | $this->backtrack($i + 1, $k, $cur, $nums, $res); 39 | 40 | array_pop($cur); 41 | } 42 | 43 | } 44 | } 45 | 46 | // Accepted 47 | // 10/10 cases passed (4 ms) 48 | // Your runtime beats 100 % of php submissions 49 | // Your memory usage beats 100 % of php submissions (15.4 MB) 50 | // @lc code=end 51 | 52 | -------------------------------------------------------------------------------- /08th分治、回溯/78.子集_递归.php: -------------------------------------------------------------------------------- 1 | val = $value; } 16 | * } 17 | */ 18 | class Solution { 19 | 20 | /** 21 | * @param TreeNode $root 22 | * @return Integer[][] 23 | */ 24 | function levelOrder($root) { 25 | 26 | if($root == null) return []; 27 | 28 | $res = []; 29 | $queue = []; 30 | array_push($queue, $root); 31 | 32 | while ($n = count($queue)) { 33 | $tmp = []; 34 | 35 | for ($i=0; $i < $n; $i++) { 36 | // 先进先出 37 | $cur = array_shift($queue); 38 | $tmp[] = $cur->val; 39 | 40 | if($cur->left != null) array_push($queue, $cur->left); 41 | if($cur->right != null) array_push($queue, $cur->right); 42 | } 43 | 44 | $res[] = $tmp; 45 | } 46 | 47 | return $res; 48 | } 49 | } 50 | 51 | // Accepted 52 | // 34/34 cases passed (8 ms) 53 | // Your runtime beats 87.32 % of php submissions 54 | // Your memory usage beats 100 % of php submissions (15.5 MB) 55 | // @lc code=end 56 | 57 | -------------------------------------------------------------------------------- /09th深度优先搜索和广度优先搜索/127.单词接龙_超时.php: -------------------------------------------------------------------------------- 1 | row = count($grid); 25 | $this->col = count($grid[0]); 26 | 27 | for ($i=0; $i < $this->row; $i++) { 28 | 29 | for ($j=0; $j < $this->col; $j++) { 30 | 31 | if($grid[$i][$j] == '1'){ 32 | $count++; 33 | $this->dfs($grid, $i, $j); 34 | } 35 | } 36 | } 37 | 38 | return $count; 39 | } 40 | 41 | function dfs(&$grid, $i, $j) { 42 | 43 | if($i < 0 || $j < 0 || $i >= $this->row || $j >= $this->col || $grid[$i][$j] == '0') { 44 | return true; 45 | } 46 | 47 | $grid[$i][$j] = '0'; 48 | $this->dfs($grid, $i - 1, $j); 49 | $this->dfs($grid, $i + 1, $j); 50 | $this->dfs($grid, $i, $j - 1); 51 | $this->dfs($grid, $i, $j + 1); 52 | } 53 | } 54 | 55 | // Accepted 56 | // 47/47 cases passed (56 ms) 57 | // Your runtime beats 51.2 % of php submissions 58 | // Your memory usage beats 100 % of php submissions (22.6 MB) 59 | // @lc code=end 60 | 61 | -------------------------------------------------------------------------------- /09th深度优先搜索和广度优先搜索/22.括号生成_BFS.php: -------------------------------------------------------------------------------- 1 | 0) { 38 | array_push($queue, [$str."(", $left - 1, $right]); 39 | } 40 | 41 | if($right > 0 && $left < $right) { 42 | array_push($queue, [$str.")", $left, $right - 1]); 43 | } 44 | 45 | } 46 | 47 | return $res; 48 | } 49 | } 50 | 51 | // Accepted 52 | // 8/8 cases passed (28 ms) 53 | // Your runtime beats 7.65 % of php submissions 54 | // Your memory usage beats 100 % of php submissions (16.1 MB) 55 | // 效率很低?? 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /09th深度优先搜索和广度优先搜索/22.括号生成_DFS.php: -------------------------------------------------------------------------------- 1 | generate(0, 0, $n, "", $res); 20 | 21 | return $res; 22 | } 23 | 24 | // 递归生成括号 25 | function generate($left, $right, $n, $s, &$res) { 26 | 27 | // 每次终止条件 28 | if($left == $n && $right == $n) { 29 | $res[] = $s; 30 | } 31 | 32 | // 左括号还没用完 33 | if($left < $n) { 34 | $this->generate($left+1, $right, $n, $s."(", $res); 35 | } 36 | 37 | // 右括号小于左口号数量时,+1 38 | if($right < $left) { 39 | $this->generate($left, $right+1, $n, $s.")", $res); 40 | } 41 | } 42 | } 43 | 44 | // Accepted 45 | // 8/8 cases passed (8 ms) 46 | // Your runtime beats 79.59 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (15.6 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /09th深度优先搜索和广度优先搜索/433.最小基因变化_BFS.php: -------------------------------------------------------------------------------- 1 | "CGT", 23 | 'C' => "AGT", 24 | 'G' => "ACT", 25 | 'T' => "ACG", 26 | ]; 27 | 28 | // 初始入队 29 | $queue = [ 30 | [$start, 0], 31 | ]; 32 | 33 | while ($n = count($queue)) { 34 | 35 | $cur = array_shift($queue); 36 | $str = $cur[0]; 37 | $step = $cur[1]; 38 | 39 | if($str == $end) return $step; 40 | 41 | for ($i=0; $i < strlen($str); $i++) { 42 | 43 | $gen = $change[$str[$i]]; 44 | 45 | for ($j=0; $j < strlen($gen); $j++) { 46 | 47 | $new = substr($str, 0, $i).$gen[$j].substr($str, $i+1); 48 | 49 | if(in_array($new, $bank)){ 50 | array_push($queue, [$new, $step + 1]); 51 | // 剪枝,避免重复 52 | //unset($bank[array_search($bank)]); 53 | } 54 | } 55 | } 56 | } 57 | 58 | return -1; 59 | } 60 | } 61 | 62 | // Accepted 63 | // 14/14 cases passed (8 ms) 64 | // Your runtime beats 68.75 % of php submissions 65 | // Your memory usage beats 100 % of php submissions (15 MB) 66 | // @lc code=end 67 | 68 | -------------------------------------------------------------------------------- /09th深度优先搜索和广度优先搜索/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /10th贪心算法/122.买卖股票的最佳时机-ii.php: -------------------------------------------------------------------------------- 1 | $prices[$i-1]) { 22 | $max_profit += $prices[$i] - $prices[$i-1]; 23 | } 24 | } 25 | 26 | return $max_profit; 27 | } 28 | } 29 | 30 | // Accepted 31 | // 200/200 cases passed (20 ms) 32 | // Your runtime beats 69.88 % of php submissions 33 | // Your memory usage beats 100 % of php submissions (16.9 MB) 34 | // @lc code=end 35 | 36 | -------------------------------------------------------------------------------- /10th贪心算法/455.分发饼干.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 23 | 24 | if($i + $nums[$i] >= $enableIndex) { 25 | $enableIndex = $i; 26 | } 27 | } 28 | 29 | return $enableIndex == 0; 30 | } 31 | } 32 | 33 | // Accepted 34 | // 75/75 cases passed (28 ms) 35 | // Your runtime beats 75.43 % of php submissions 36 | // Your memory usage beats 100 % of php submissions (18.4 MB) 37 | // @lc code=end 38 | 39 | -------------------------------------------------------------------------------- /10th贪心算法/55.跳跃游戏_贪心2.php: -------------------------------------------------------------------------------- 1 | $jump) { 22 | 23 | // 如果当前位置能到达,并且当前位置+跳数>最远位置 24 | if($max_i >= $i && $i + $jump > $max_i) { 25 | $max_i = $i + $jump; 26 | } 27 | } 28 | 29 | return $max_i >= $i; 30 | } 31 | } 32 | 33 | // Accepted 34 | // 75/75 cases passed (28 ms) 35 | // Your runtime beats 75.43 % of php submissions 36 | // Your memory usage beats 100 % of php submissions (18.1 MB) 37 | // @lc code=end 38 | 39 | -------------------------------------------------------------------------------- /10th贪心算法/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /11th二分查找/33.搜索旋转排序数组.php: -------------------------------------------------------------------------------- 1 | > 1); 23 | 24 | if($nums[$mid] == $target) { 25 | return $mid; 26 | } 27 | 28 | if($nums[$start] <= $nums[$mid]) { 29 | // 左半边有序 30 | if($nums[$start] <= $target && $target < $nums[$mid]){ 31 | // 目标值落在有序区间时,搜索有序区间,否则搜索无序区间 32 | $end = $mid - 1; 33 | }else{ 34 | $start = $mid + 1; 35 | } 36 | }else{ 37 | // 右半边有序 38 | if($nums[$mid] < $target && $target <= $nums[$end]){ 39 | $start = $mid + 1; 40 | }else{ 41 | $end = $mid - 1; 42 | } 43 | } 44 | } 45 | 46 | return -1; 47 | } 48 | } 49 | 50 | // Accepted 51 | // 196/196 cases passed (16 ms) 52 | // Your runtime beats 34.91 % of php submissions 53 | // Your memory usage beats 100 % of php submissions (15 MB) 54 | // @lc code=end 55 | 56 | -------------------------------------------------------------------------------- /11th二分查找/367.有效的完全平方数.php: -------------------------------------------------------------------------------- 1 | 0) { 19 | $num -= $i; 20 | $i += 2; 21 | } 22 | 23 | return $num == 0; 24 | } 25 | } 26 | 27 | // 效率低,但是很秀, 28 | // 1 4=1+3 9=1+3+5 16=1+3+5+7以此类推 29 | // Accepted 30 | // 70/70 cases passed (12 ms) 31 | // Your runtime beats 13.21 % of php submissions 32 | // Your memory usage beats 100 % of php submissions (14.8 MB) 33 | // @lc code=end 34 | 35 | -------------------------------------------------------------------------------- /11th二分查找/69.x-的平方根.php: -------------------------------------------------------------------------------- 1 | $x){ 27 | $right = $mid - 1; 28 | } else if($num < $x) { 29 | $left = $mid + 1; 30 | } else { 31 | return $mid; 32 | } 33 | } 34 | 35 | return $right; 36 | } 37 | } 38 | 39 | // Accepted 40 | // 1017/1017 cases passed (8 ms) 41 | // Your runtime beats 90.3 % of php submissions 42 | // Your memory usage beats 100 % of php submissions (14.7 MB) 43 | // @lc code=end 44 | 45 | -------------------------------------------------------------------------------- /11th二分查找/74.搜索二维矩阵.php: -------------------------------------------------------------------------------- 1 | = 0 && $col < count($matrix[0])) { 27 | if($matrix[$row][$col] == $target) { 28 | return true; 29 | } 30 | 31 | if($matrix[$row][$col] < $target) { 32 | $col++; 33 | }else { 34 | $row--; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 136/136 cases passed (24 ms) 44 | // Your runtime beats 76 % of php submissions 45 | // Your memory usage beats 100 % of php submissions (16.7 MB) 46 | // @lc code=end 47 | -------------------------------------------------------------------------------- /11th二分查找/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /12th动态规划/1143.最长公共子序列.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 25 | for ($j=0; $j <= $i; $j++) { 26 | // 当前坐标的最小路径和 = 下一层的同坐标和相邻坐标(较小值) + 当前坐标值 27 | $dp[$i][$j] = min($dp[$i + 1][$j], $dp[$i + 1][$j + 1]) + $triangle[$i][$j]; 28 | } 29 | } 30 | 31 | return $dp[0][0]; 32 | } 33 | } 34 | 35 | // Accepted 36 | // 43/43 cases passed (16 ms) 37 | // Your runtime beats 91.3 % of php submissions 38 | // Your memory usage beats 100 % of php submissions (17.6 MB) 39 | // @lc code=end 40 | 41 | -------------------------------------------------------------------------------- /12th动态规划/120.三角形最小路径和_优化内存.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 25 | for ($j=0; $j <= $i; $j++) { 26 | $dp[$j] = min($dp[$j], $dp[$j + 1]) + $triangle[$i][$j]; 27 | } 28 | } 29 | 30 | return $dp[0]; 31 | } 32 | } 33 | 34 | // Accepted 35 | // 43/43 cases passed (12 ms) 36 | // Your runtime beats 99.03 % of php submissions 37 | // Your memory usage beats 100 % of php submissions (16.2 MB) 38 | // @lc code=end 39 | 40 | -------------------------------------------------------------------------------- /12th动态规划/121.买卖股票的最佳时机.php: -------------------------------------------------------------------------------- 1 | dp = array_fill(0, $n+2, array_fill(0, $n+2, 0)); 27 | $res = $this->helper($nums, 1, $n); 28 | return $res; 29 | } 30 | 31 | function helper(&$nums, $i, $j) { 32 | // boundary 33 | if($i > $j) return 0; 34 | if($this->dp[$i][$j] > 0) return $this->dp[$i][$j]; 35 | 36 | // search 37 | // 优先戳破左边和右边的气球,再戳破当前气球,可以看出左右两个子问题是独立的,并且只和当前气球关联 38 | // 每个子问题里,k 的范围是 i => j 39 | // 定义状态转移方程:dp[i][j] = dp[i][k-1] + dp[k+1][j] + nums[k] * nums[i-1] * nums[j+1] 40 | for ($k=$i; $k <= $j; $k++) { 41 | $left = $this->helper($nums, $i, $k - 1); 42 | $right = $this->helper($nums, $k + 1, $j); 43 | $delta = $nums[$k] * $nums[$i - 1] * $nums[$j + 1]; 44 | 45 | $this->dp[$i][$j] = max($this->dp[$i][$j], $left + $right + $delta); 46 | } 47 | 48 | return $this->dp[$i][$j]; 49 | } 50 | } 51 | 52 | // Accepted 53 | // 70/70 cases passed (288 ms) 54 | // Your runtime beats 84.62 % of php submissions 55 | // Your memory usage beats 100 % of php submissions (16.5 MB) 56 | // @lc code=end -------------------------------------------------------------------------------- /12th动态规划/32.最长有效括号.php: -------------------------------------------------------------------------------- 1 | 0 && $s[$i - $dp[$i - 1] - 1] == '(') { 30 | // 当前有效长度 + 前一个有效长度 + 前前一个有效长度 31 | $dp[$i] = 2 + $dp[$i - 1] + ($i - $dp[$i - 1] - 2 > -1 ? $dp[$i - $dp[$i - 1] - 2] : 0); 32 | } 33 | } 34 | 35 | $max = max($max, $dp[$i]); 36 | } 37 | 38 | return $max; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 230/230 cases passed (12 ms) 44 | // Your runtime beats 87.5 % of php submissions 45 | // Your memory usage beats 100 % of php submissions (15.3 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /12th动态规划/322.零钱兑换_自底向上DP.php: -------------------------------------------------------------------------------- 1 | $amount ? -1 : $dp[$amount]; 37 | } 38 | } 39 | 40 | // 1. 暴力:递归、指数级 41 | // 2. BFS 42 | // 3. DP array: f(n) = min{f(n - k), for k in [1, 2, 5]} + 1 43 | 44 | // Accepted 45 | // 182/182 cases passed (368 ms) 46 | // Your runtime beats 42.31 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (14.8 MB) 48 | // @lc code=end -------------------------------------------------------------------------------- /12th动态规划/53.最大子序和.php: -------------------------------------------------------------------------------- 1 | 0 ? $sum + $num : $num; 26 | $ans = max($ans, $sum); 27 | } 28 | 29 | return $ans; 30 | } 31 | } 32 | 33 | // Accepted 34 | // 202/202 cases passed (24 ms) 35 | // Your runtime beats 43.03 % of php submissions 36 | // Your memory usage beats 100 % of php submissions (16.2 MB) 37 | // @lc code=end 38 | 39 | -------------------------------------------------------------------------------- /12th动态规划/53.最大子序和_DP.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 35 | // 逐步减去矩形中任务的执行时间,剩下就是所需的等待时间 36 | $space = $space - min($max_n, $record[$i]); 37 | } 38 | 39 | return $space > 0 ? count($tasks) + $space : count($tasks); 40 | } 41 | } 42 | 43 | // Accepted 44 | // 69/69 cases passed (140 ms) 45 | // Your runtime beats 66.67 % of php submissions 46 | // Your memory usage beats 100 % of php submissions (16.7 MB) 47 | // @lc code=end 48 | 49 | -------------------------------------------------------------------------------- /12th动态规划/64.最小路径和.php: -------------------------------------------------------------------------------- 1 | = 0) { 30 | $dp[$i] = $dp[$i] + $dp[$i - 2]; 31 | }else { 32 | $dp[$i] = $dp[$i] + 1; 33 | } 34 | } 35 | } 36 | 37 | return $dp[$n - 1]; 38 | } 39 | } 40 | 41 | // Accepted 42 | // 258/258 cases passed (4 ms) 43 | // Your runtime beats 90.32 % of php submissions 44 | // Your memory usage beats 100 % of php submissions (14.9 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /12th动态规划/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /13th字典树和并查集/200.岛屿数量.php: -------------------------------------------------------------------------------- 1 | m = count($grid); 26 | $this->n = count($grid[0]); 27 | $count = 0; 28 | 29 | for ($i=0; $i < $this->m; $i++) { 30 | for ($j=0; $j < $this->n; $j++) { 31 | if ($grid[$i][$j] == '1') { // 当前为1的话,将其改为0,遍历相邻的四个点,遇到0退出,遇到1,dfs重复前面过程 32 | $count++; 33 | $this->dfs($grid, $i, $j); 34 | } 35 | } 36 | } 37 | 38 | return $count; 39 | } 40 | 41 | function dfs(&$grid, $i, $j) { 42 | 43 | if ($i < 0 || $i >= $this->m || $j < 0 || $j >= $this->n || $grid[$i][$j] == '0') { 44 | return true; 45 | } 46 | 47 | $grid[$i][$j] = '0'; 48 | $this->dfs($grid, $i + 1, $j); 49 | $this->dfs($grid, $i - 1, $j); 50 | $this->dfs($grid, $i, $j - 1); 51 | $this->dfs($grid, $i, $j + 1); 52 | } 53 | } 54 | 55 | // Accepted 56 | // 47/47 cases passed (52 ms) 57 | // Your runtime beats 58.33 % of php submissions 58 | // Your memory usage beats 88.89 % of php submissions (22.2 MB) 59 | // @lc code=end 60 | 61 | -------------------------------------------------------------------------------- /13th字典树和并查集/547.朋友圈_BFS.php: -------------------------------------------------------------------------------- 1 | dfs($M, $i, $visited); 29 | $res++; 30 | } 31 | 32 | return $res; 33 | } 34 | 35 | function dfs(&$M, $k, &$visited) { 36 | 37 | $visited[$k] = 1; 38 | for ($i=0; $i < count($M); $i++) { 39 | if($M[$k][$i] == 0 || $visited[$i]) continue; 40 | $this->dfs($M, $i, $visited); 41 | } 42 | } 43 | } 44 | 45 | // Accepted 46 | // 113/113 cases passed (72 ms) 47 | // Your runtime beats 91.18 % of php submissions 48 | // Your memory usage beats 100 % of php submissions (17.2 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /13th字典树和并查集/547.朋友圈_UnionFind.php: -------------------------------------------------------------------------------- 1 | getRoot($root, $i); 31 | $p2 = $this->getRoot($root, $j); 32 | if ($p1 != $p2) { 33 | $res--; 34 | $root[$p2] = $p1; 35 | } 36 | } 37 | } 38 | } 39 | 40 | return $res; 41 | } 42 | 43 | function getRoot(&$root, $i) { 44 | while ($i != $root[$i]) { 45 | $root[$i] = $root[$root[$i]]; 46 | $i = $root[$i]; 47 | } 48 | 49 | return $i; 50 | } 51 | } 52 | 53 | // Accepted 54 | // 113/113 cases passed (80 ms) 55 | // Your runtime beats 64.71 % of php submissions 56 | // Your memory usage beats 100 % of php submissions (17.1 MB) 57 | // @lc code=end 58 | 59 | -------------------------------------------------------------------------------- /13th字典树和并查集/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /14th高级搜索/22.括号生成.php: -------------------------------------------------------------------------------- 1 | res = []; 22 | 23 | $this->dfs(0, 0, $n, ""); 24 | return $this->res; 25 | } 26 | 27 | function dfs ($left, $right, $n, $s) { 28 | 29 | if ($left == $n && $right == $n) { 30 | $this->res[] = $s; 31 | } 32 | 33 | if ($left < $n) { 34 | $this->dfs($left + 1, $right, $n, $s."("); 35 | } 36 | 37 | if ($right < $left) { 38 | $this->dfs($left, $right + 1, $n, $s.")"); 39 | } 40 | } 41 | } 42 | 43 | // Accepted 44 | // 8/8 cases passed (4 ms) 45 | // Your runtime beats 99.3 % of php submissions 46 | // Your memory usage beats 25 % of php submissions (15.5 MB) 47 | // @lc code=end 48 | 49 | -------------------------------------------------------------------------------- /14th高级搜索/70.爬楼梯.php: -------------------------------------------------------------------------------- 1 | > 1; 23 | } 24 | 25 | return $ans; 26 | } 27 | } 28 | 29 | // Accepted 30 | // 600/600 cases passed (4 ms) 31 | // Your runtime beats 98.21 % of php submissions 32 | // Your memory usage beats 57.14 % of php submissions (14.7 MB) 33 | // @lc code=end 34 | 35 | -------------------------------------------------------------------------------- /16th位运算/191.位-1-的个数.php: -------------------------------------------------------------------------------- 1 | 0 && ($n & $n - 1) == 0; 21 | } 22 | } 23 | 24 | // Accepted 25 | // 1108/1108 cases passed (24 ms) 26 | // Your runtime beats 7.78 % of php submissions 27 | // Your memory usage beats 88.24 % of php submissions (14.7 MB) 28 | // @lc code=end 29 | 30 | -------------------------------------------------------------------------------- /16th位运算/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /17th布隆过滤器和 LRU 缓存/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /18th排序算法/1122.数组的相对排序.php: -------------------------------------------------------------------------------- 1 | 0) { 30 | $arr1[$i++] = $num2; 31 | $count[$num2] -= 1; 32 | } 33 | } 34 | } 35 | 36 | ksort($count); // 剩下的数排好序 37 | foreach ($count as $num1 => $n) { // 顺序插入 arr1 38 | while ($n > 0) { 39 | $arr1[$i++] = $num1; 40 | $n--; 41 | } 42 | } 43 | 44 | return $arr1; 45 | } 46 | } 47 | 48 | // Accepted 49 | // 16/16 cases passed (8 ms) 50 | // Your runtime beats 78.57 % of php submissions 51 | // Your memory usage beats 14.29 % of php submissions (15.1 MB) 52 | // @lc code=end 53 | 54 | -------------------------------------------------------------------------------- /18th排序算法/242.有效的字母异位词.php: -------------------------------------------------------------------------------- 1 | 0 && $s[$i - $dp[$i - 1] - 1] == '(') { 30 | // 当前有效长度 + 前一个有效长度 + 前前一个有效长度 31 | $dp[$i] = 2 + $dp[$i - 1] + ($i - $dp[$i - 1] - 2 > -1 ? $dp[$i - $dp[$i - 1] - 2] : 0); 32 | } 33 | } 34 | 35 | $max = max($max, $dp[$i]); 36 | } 37 | 38 | return $max; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 230/230 cases passed (12 ms) 44 | // Your runtime beats 88.7 % of php submissions 45 | // Your memory usage beats 17.39 % of php submissions (15.7 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /19th高级动态规划/818.赛车_超时.php: -------------------------------------------------------------------------------- 1 | 1, 25 | "0_-1" => 1 26 | ]; 27 | $steps = 0; 28 | while (!empty($q)) { 29 | $size = count($q); 30 | while ($size--) { 31 | $p = array_shift($q); 32 | $pos = $p[0]; 33 | $speed = $p[1]; 34 | 35 | $pos1 = $pos + $speed; 36 | $speed1 = $speed * 2; 37 | $p1 = [$pos1, $speed1]; 38 | if ($pos1 == $target) return $steps + 1; 39 | if ($p1[0] > 0 && $p1[0] < 2 * $target) { // 剪枝 40 | array_push($q, $p1); 41 | } 42 | 43 | $speed2 = $speed > 0 ? -1 : 1; 44 | $p2 = [$pos, $speed2]; 45 | $key2 = (string)($pos."_".$speed2); 46 | if (!array_key_exists($key2, $v)) { 47 | array_push($q, $p2); 48 | $v[$key2] = 1; 49 | } 50 | } 51 | $steps++; 52 | } 53 | 54 | return -1; 55 | } 56 | } 57 | // @lc code=end 58 | 59 | -------------------------------------------------------------------------------- /19th高级动态规划/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /20th字符串算法/151.翻转字符串里的单词.php: -------------------------------------------------------------------------------- 1 | = 0; $i--) { 25 | if(trim($str_arr[$i]) == "") continue; 26 | $str .= trim($str_arr[$i])." "; 27 | } 28 | 29 | return substr($str, 0, -1); 30 | } 31 | } 32 | 33 | // Accepted 34 | // 25/25 cases passed (8 ms) 35 | // Your runtime beats 88.46 % of php submissions 36 | // Your memory usage beats 100 % of php submissions (15.1 MB) 37 | // @lc code=end 38 | 39 | -------------------------------------------------------------------------------- /20th字符串算法/205.同构字符串.php: -------------------------------------------------------------------------------- 1 | t 、t -> s 两个方向,均为同构才为真 21 | // 例如 s = "ab"、t = "aa" 22 | return $this->helper($s, $t) && $this->helper($t, $s); 23 | } 24 | 25 | function helper($s, $t) { 26 | 27 | $len = strlen($s); 28 | $map = []; 29 | for ($i=0; $i < $len; $i++) { 30 | if (array_key_exists($s[$i], $map)) { 31 | if ($map[$s[$i]] != $t[$i]) return false; 32 | } else { 33 | $map[$s[$i]] = $t[$i]; 34 | } 35 | } 36 | 37 | return true; 38 | } 39 | } 40 | 41 | // Accepted 42 | // 32/32 cases passed (12 ms) 43 | // Your runtime beats 68 % of php submissions 44 | // Your memory usage beats 87.5 % of php submissions (14.8 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /20th字符串算法/387.字符串中的第一个唯一字符.php: -------------------------------------------------------------------------------- 1 | = $p_len) --$vs[ord($s[$i - $p_len]) - ord('a')]; 36 | ++$vs[ord($s[$i]) - ord('a')]; 37 | if ($vs == $vp) $ans[] = $i + 1 - $p_len; 38 | } 39 | 40 | return $ans; 41 | } 42 | } 43 | 44 | // Accepted 45 | // 36/36 cases passed (40 ms) 46 | // Your runtime beats 33.33 % of php submissions 47 | // Your memory usage beats 100 % of php submissions (15.8 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /20th字符串算法/541.反转字符串-ii.php: -------------------------------------------------------------------------------- 1 | =pow(2, 31)) { 33 | return pow(2, 31)-1; 34 | }elseif($str< -pow(2, 31)){ 35 | return -pow(2, 31); 36 | } 37 | return intval($str); 38 | } 39 | } 40 | } 41 | 42 | // Accepted 43 | // 1079/1079 cases passed (8 ms) 44 | // Your runtime beats 83.33 % of php submissions 45 | // Your memory usage beats 33.33 % of php submissions (15 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /20th字符串算法/README.md: -------------------------------------------------------------------------------- 1 | ### 专题知识点 -------------------------------------------------------------------------------- /21剑指offer系列/README.md: -------------------------------------------------------------------------------- 1 | ### 剑指offer系列 2 | 3 | https://leetcode-cn.com/problemset/lcof/ 4 | -------------------------------------------------------------------------------- /22程序员面试经典/README.md: -------------------------------------------------------------------------------- 1 | ### 程序员面试经典 2 | 3 | https://leetcode-cn.com/problemset/lcci/ 4 | -------------------------------------------------------------------------------- /23刷题路线/128.最长连续序列.php: -------------------------------------------------------------------------------- 1 | $val) { 27 | // 剪掉 x - 1 的无意义查询 28 | if (!array_key_exists($key - 1, $num_set)) { // 键查询更快 29 | $currentNum = $key; 30 | $currentStreak = 1; 31 | 32 | while (array_key_exists($currentNum + 1, $num_set)) { 33 | $currentNum += 1; 34 | $currentStreak += 1; 35 | } 36 | 37 | $longestStreak = max($longestStreak, $currentStreak); 38 | } 39 | } 40 | 41 | return $longestStreak; 42 | } 43 | } 44 | 45 | // Accepted 46 | // 68/68 cases passed (24 ms) 47 | // Your runtime beats 22.46 % of php submissions 48 | // Your memory usage beats 57.14 % of php submissions (15.9 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /23刷题路线/[基础]1.两数之和.php: -------------------------------------------------------------------------------- 1 | $num) { 22 | 23 | if (array_key_exists($target - $num, $tmp)) { 24 | if ($nums[$tmp[$target - $num]] + $num === $target) { 25 | return [$tmp[$target - $num], $k]; 26 | } 27 | } 28 | 29 | $tmp[$num] = $k; 30 | } 31 | 32 | return []; 33 | } 34 | } 35 | 36 | // Accepted 37 | // 29/29 cases passed (16 ms) 38 | // Your runtime beats 80.43 % of php submissions 39 | // Your memory usage beats 22.89 % of php submissions (16 MB) 40 | // @lc code=end 41 | 42 | -------------------------------------------------------------------------------- /23刷题路线/[基础]20.有效的括号.php: -------------------------------------------------------------------------------- 1 | "]", 21 | "{" => "}", 22 | "(" => ")", 23 | "?" => "?" 24 | ]; 25 | 26 | $stack = ["?"]; 27 | 28 | for ($i=0; $i < strlen($s); $i++) { 29 | 30 | if (array_key_exists($s[$i], $map)) { // 左括号入栈 31 | $stack[] = $s[$i]; 32 | } elseif ($map[array_pop($stack)] != $s[$i]) { // 非左括号出栈判断是否右括号 33 | return false; 34 | } 35 | } 36 | 37 | return count($stack) == 1; 38 | } 39 | } 40 | 41 | // Accepted 42 | // 91/91 cases passed (8 ms) 43 | // Your runtime beats 77.9 % of php submissions 44 | // Your memory usage beats 40.68 % of php submissions (15.1 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leetcode 精选题目&题解 2 | 3 | ### 如何做题? 4 | 5 | - https://leetcode-cn.com/problemset/all/ 搜索对应题号即可 6 | 7 | - https://www.cnblogs.com/gl1573/p/11262333.html 如何在 vscode 上做题并测试、提交 8 | 9 | ### 刷题技巧 10 | 11 | 误区:一道题只做一遍 12 | 13 | 建议:一道题最好独立做五遍以上,第一遍如果没有思路,可以借鉴题解区或者评论区的优秀解法,切忌死磕,保证测试用例完全通过;第二遍整理思路,靠自己手写;隔一段时间做第三遍、第四遍、第五遍;坚持刻意练习,以达到强化算法思维训练的目的。 14 | 15 | ### 文章 16 | 17 | [算法学习之旅,终点亦是起点](https://learnku.com/articles/49105) 18 | -------------------------------------------------------------------------------- /每日1题202007/README.md: -------------------------------------------------------------------------------- 1 | # leetcode 每日1题 -------------------------------------------------------------------------------- /每日1题202007/[0713]350.两个数组的交集-ii.php: -------------------------------------------------------------------------------- 1 | $nums2[$idx2]) { 37 | $idx2++; 38 | }else{ 39 | $idx1++; 40 | } 41 | } 42 | 43 | return $res; 44 | } 45 | } 46 | 47 | // Accepted 48 | // 61/61 cases passed (16 ms) 49 | // Your runtime beats 74.03 % of php submissions 50 | // Your memory usage beats 50 % of php submissions (15.3 MB) 51 | // @lc code=end 52 | 53 | -------------------------------------------------------------------------------- /每日1题202007/[0715]96.不同的二叉搜索树.php: -------------------------------------------------------------------------------- 1 | > 1); 34 | 35 | // 严格小于 target 的元素一定不是解 36 | if($nums[$mid] < $target){ 37 | $start = $mid + 1; 38 | }else{ 39 | $end = $mid; 40 | } 41 | } 42 | 43 | return $start; 44 | } 45 | } 46 | 47 | // Accepted 48 | // 62/62 cases passed (16 ms) 49 | // Your runtime beats 60.71 % of php submissions 50 | // Your memory usage beats 100 % of php submissions (15.6 MB) 51 | // @lc code=end 52 | 53 | -------------------------------------------------------------------------------- /每日1题202007/[0719]312.戳气球_动态规划DP.php: -------------------------------------------------------------------------------- 1 | dp = array_fill(0, $n+2, array_fill(0, $n+2, 0)); 27 | $res = $this->helper($nums, 1, $n); 28 | return $res; 29 | } 30 | 31 | function helper(&$nums, $i, $j) { 32 | // boundary 33 | if($i > $j) return 0; 34 | if($this->dp[$i][$j] > 0) return $this->dp[$i][$j]; 35 | 36 | // search 37 | // 优先戳破左边和右边的气球,再戳破当前气球,可以看出左右两个子问题是独立的,并且只和当前气球关联 38 | // 每个子问题里,k 的范围是 i => j 39 | // 定义状态转移方程:dp[i][j] = dp[i][k-1] + dp[k+1][j] + nums[k] * nums[i-1] * nums[j+1] 40 | for ($k=$i; $k <= $j; $k++) { 41 | $left = $this->helper($nums, $i, $k - 1); 42 | $right = $this->helper($nums, $k + 1, $j); 43 | $delta = $nums[$k] * $nums[$i - 1] * $nums[$j + 1]; 44 | 45 | $this->dp[$i][$j] = max($this->dp[$i][$j], $left + $right + $delta); 46 | } 47 | 48 | return $this->dp[$i][$j]; 49 | } 50 | } 51 | 52 | // Accepted 53 | // 70/70 cases passed (288 ms) 54 | // Your runtime beats 84.62 % of php submissions 55 | // Your memory usage beats 100 % of php submissions (16.5 MB) 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /每日1题202007/[0719]312.戳气球_回溯法(超时).php: -------------------------------------------------------------------------------- 1 | helper($nums, 0, $res); 21 | return $res; 22 | } 23 | 24 | function helper(&$nums, $coins, &$res) { 25 | $len = count($nums); 26 | 27 | // boundary 28 | if($len == 0) { 29 | $res = max($res, $coins); 30 | return true; 31 | } 32 | 33 | // search 34 | // 每一层可选择戳破的气球数 35 | for ($i=0; $i < $len; $i++) { 36 | $delta = $nums[$i] * ($i - 1 < 0 ? 1 : $nums[$i-1]) * 37 | ($i + 1 > $len - 1 ? 1 : $nums[$i+1]); 38 | 39 | $nums_copy = $nums; 40 | unset($nums_copy[$i]); 41 | $nums_copy = array_merge($nums_copy, []); 42 | $this->helper($nums_copy, $coins + $delta, $res); 43 | } 44 | } 45 | 46 | // function array_insert($arr, $value, $position = 0) { 47 | 48 | // $fore = ($position == 0) ? array() : array_splice($arr, 0, $position); 49 | // $fore[] = $value; 50 | // $ret = array_merge($fore, $arr); 51 | 52 | // return $ret; 53 | // } 54 | } 55 | 56 | // 回溯法,超时 57 | // Time Limit Exceeded 58 | // 20/70 cases passed (N/A) 59 | 60 | // Time Limit Exceeded 61 | // 24/70 cases passed (N/A) 62 | // @lc code=end 63 | 64 | -------------------------------------------------------------------------------- /每日1题202007/[0720]167.两数之和-ii-输入有序数组.php: -------------------------------------------------------------------------------- 1 | $v) { 22 | 23 | if(isset($result[$target - $v])){ 24 | return [$result[$target - $v], $k+1]; 25 | } 26 | 27 | $result[$v] = $k+1; 28 | } 29 | } 30 | } 31 | 32 | // Accepted 33 | // 17/17 cases passed (12 ms) 34 | // Your runtime beats 97.3 % of php submissions 35 | // Your memory usage beats 100 % of php submissions (16.6 MB) 36 | // @lc code=end 37 | 38 | -------------------------------------------------------------------------------- /每日1题202007/[0722]剑指 Offer 11. 旋转数组的最小数字.php: -------------------------------------------------------------------------------- 1 | = 1){ 29 | 30 | if($numbers[$end] < $numbers[$end - 1]) { 31 | return $numbers[$end]; 32 | } 33 | 34 | if($numbers[$end] == $numbers[$end - 1] && $end - 2 < 0) { 35 | return $numbers[$end]; 36 | } 37 | 38 | $end--; 39 | } 40 | } 41 | } 42 | 43 | // 执行结果:通过 44 | // 执行用时: 12 ms, 在所有 PHP 提交中击败了 97.59% 的用户 45 | // 内存消耗: 15.5 MB, 在所有 PHP 提交中击败了 100.00% 的用户 -------------------------------------------------------------------------------- /每日1题202007/[0723]64.最小路径和.php: -------------------------------------------------------------------------------- 1 | divisorGame($N - 1); 24 | } 25 | } 26 | 27 | // Accepted 28 | // 40/40 cases passed (4 ms) 29 | // Your runtime beats 100 % of php submissions 30 | // Your memory usage beats 100 % of php submissions (15.3 MB) 31 | // @lc code=end 32 | 33 | -------------------------------------------------------------------------------- /每日1题202007/[0727]392.判断子序列.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer 25 | */ 26 | function maxDepth($root) { 27 | 28 | if($root == null) { 29 | return 0; 30 | } 31 | 32 | if($root->left == null && $root->right == null) { 33 | return 1; 34 | } 35 | 36 | if($root->left != null) { 37 | $left = $this->maxDepth($root->left); 38 | } 39 | 40 | if($root->right != null) { 41 | $right = $this->maxDepth($root->right); 42 | } 43 | 44 | return max($left, $right) + 1; 45 | } 46 | } 47 | // @lc code=end 48 | 49 | -------------------------------------------------------------------------------- /每日1题202007/[0730]343.整数拆分.php: -------------------------------------------------------------------------------- 1 | val = $val; 20 | * $this->left = $left; 21 | * $this->right = $right; 22 | * } 23 | * } 24 | */ 25 | class Solution { 26 | 27 | /** 28 | * @param TreeNode $root 29 | * @return NULL 30 | */ 31 | function flatten($root) { 32 | 33 | while ($root != null) { 34 | if ($root->left != null) { 35 | // 找到左子树最右边的节点 36 | $pre = $root->left; 37 | while ($pre->right != null) { 38 | $pre = $pre->right; 39 | } 40 | 41 | // 将原来的右子树接到左子树的最右边节点 42 | $pre->right = $root->right; 43 | $root->right = $root->left; 44 | $root->left = null; 45 | } 46 | 47 | // 考虑下一个节点 48 | $root = $root->right; 49 | } 50 | } 51 | } 52 | 53 | // Accepted 54 | // 225/225 cases passed (8 ms) 55 | // Your runtime beats 88.24 % of php submissions 56 | // Your memory usage beats 25 % of php submissions (15.3 MB) 57 | // @lc code=end 58 | 59 | -------------------------------------------------------------------------------- /每日1题202008/[0805]337.打家劫舍-iii.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer 25 | */ 26 | function rob($root) { 27 | $root_status = $this->dfs($root); 28 | return max($root_status[0], $root_status[1]); 29 | } 30 | 31 | // dfs 32 | function dfs($node) { 33 | if ($node == null) return [0,0]; 34 | 35 | $l = $this->dfs($node->left); 36 | $r = $this->dfs($node->right); 37 | 38 | // 选择当前节点时,子节点都不可以选择 39 | $selected = $node->val + $l[0] + $r[0]; 40 | 41 | // 不选当前节点时,子节点都可以选择 42 | $not_selected = max($l[0], $l[1]) + max($r[0], $r[1]); 43 | 44 | return [$not_selected, $selected]; 45 | } 46 | } 47 | 48 | // Accepted 49 | // 124/124 cases passed (16 ms) 50 | // Your runtime beats 76.47 % of php submissions 51 | // Your memory usage beats 100 % of php submissions (17 MB) 52 | // @lc code=end 53 | 54 | -------------------------------------------------------------------------------- /每日1题202008/[0807]100.相同的树.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->left = $left; 20 | * $this->right = $right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | 26 | /** 27 | * @param TreeNode $p 28 | * @param TreeNode $q 29 | * @return Boolean 30 | */ 31 | function isSameTree($p, $q) { 32 | 33 | if($p == null && $q == null) return true; 34 | if($p == null || $q == null) return false; 35 | if($q->val != $p->val) return false; 36 | 37 | return $this->isSameTree($p->left, $q->left) && $this->isSameTree($p->right, $q->right); 38 | } 39 | } 40 | 41 | // Accepted 42 | // 59/59 cases passed (4 ms) 43 | // Your runtime beats 92.77 % of php submissions 44 | // Your memory usage beats 8.33 % of php submissions (15.2 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /每日1题202008/[0808]99.恢复二叉搜索树.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->left = $left; 20 | * $this->right = $right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | 26 | protected $t1; 27 | protected $t2; 28 | protected $pre; 29 | 30 | /** 31 | * @param TreeNode $root 32 | * @return NULL 33 | */ 34 | function recoverTree($root) { 35 | 36 | // 中序遍历,左根右一定是从小到大,出现前者比后者大的时候,记录该位置,最终交换两个点 37 | $this->inorder($root); 38 | 39 | $temp = $this->t1->val; 40 | $this->t1->val = $this->t2->val; 41 | $this->t2->val = $temp; 42 | } 43 | 44 | function inorder($root) { 45 | 46 | if ($root == null) return true; 47 | 48 | $this->inorder($root->left); 49 | 50 | if ($this->pre != null && $this->pre->val > $root->val) { 51 | if ($this->t1 == null) { 52 | $this->t1 = $this->pre; 53 | } 54 | $this->t2 = $root; 55 | } 56 | 57 | $this->pre = $root; 58 | $this->inorder($root->right); 59 | } 60 | } 61 | 62 | // Accepted 63 | // 1917/1917 cases passed (24 ms) 64 | // Your runtime beats 66.67 % of php submissions 65 | // Your memory usage beats 100 % of php submissions (15.4 MB) 66 | // @lc code=end 67 | 68 | -------------------------------------------------------------------------------- /每日1题202008/[0809]93.复原ip地址.php: -------------------------------------------------------------------------------- 1 | res = []; 22 | $this->restore(0, '', $s); 23 | return $this->res; 24 | } 25 | 26 | function restore($count, $ip, $s) { 27 | 28 | if ($count == 4) { 29 | // 当深度到达第四层时,s已经截取完且符合规则,将此ip加入最终结果集 30 | if ($s == '') { 31 | $this->res[] = substr($ip, 0, -1); 32 | } 33 | return true; 34 | } 35 | 36 | // 第一个ip段长度为 1 37 | if(strlen($s) > 0) { 38 | $this->restore($count + 1, $ip.$s[0].'.', substr($s, 1)); 39 | } 40 | 41 | // 第一个ip段长度为 2,合法ip 首位不为 0 42 | if(strlen($s) > 1 && $s[0] != '0') { // python写法 s[0:2] s[2:] 43 | $this->restore($count + 1, $ip.substr($s, 0, 2).'.', substr($s, 2)); 44 | } 45 | 46 | // 第一个ip段长度为 3,合法ip 首位不为 0 47 | if(strlen($s) > 2 && $s[0] != '0' && (int)substr($s, 0, 3) < 256) { // python写法 s[0:3] s[3:] 48 | $this->restore($count + 1, $ip.substr($s, 0, 3).'.', substr($s, 3)); 49 | } 50 | } 51 | } 52 | 53 | // Accepted 54 | // 147/147 cases passed (4 ms) 55 | // Your runtime beats 95.65 % of php submissions 56 | // Your memory usage beats 66.67 % of php submissions (14.8 MB) 57 | // @lc code=end 58 | 59 | -------------------------------------------------------------------------------- /每日1题202008/[0810]696.计数二进制子串.php: -------------------------------------------------------------------------------- 1 | = cur,n++ 是什么意思呢? 21 | // 假设有一个数 000111,从头开始遍历,到达 1 时,0 的计数时,pre 计数可以达到 3 22 | // 当出现 0001 时,cur == 1,pre > cur,包含 01 子串,符合条件,n++; 23 | // 当出现 00011 时,cur == 2,pre > cur,包含 0011 子串,符合条件,n++; 24 | // 当出现 000111 时,cur == 3,pre == cur,包含 000111 子串,符合条件,n++; 25 | // 当 1 后面开始出现 0 时,同理。 26 | 27 | $n = 0; 28 | $pre = 0; 29 | $cur = 1; 30 | $len = strlen($s) - 1; 31 | 32 | for ($i=0; $i < $len; $i++) { 33 | if ($s[$i] == $s[$i+1]) { 34 | $cur++; 35 | } else { 36 | $pre = $cur; 37 | $cur = 1; 38 | } 39 | 40 | if ($pre >= $cur) { 41 | $n++; 42 | } 43 | } 44 | 45 | return $n; 46 | } 47 | } 48 | 49 | // Accepted 50 | // 90/90 cases passed (48 ms) 51 | // Your runtime beats 100 % of php submissions 52 | // Your memory usage beats 100 % of php submissions (15 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202008/[0812]133.克隆图.php: -------------------------------------------------------------------------------- 1 | val = $val; 18 | * $this->neighbors = array(); 19 | * } 20 | * } 21 | */ 22 | 23 | class Solution { 24 | /** 25 | * @param Node $node 26 | * @return Node 27 | */ 28 | function cloneGraph($node) { 29 | $s = serialize($node); 30 | return unserialize($s); 31 | } 32 | } 33 | 34 | // Accepted 35 | // 21/21 cases passed (12 ms) 36 | // Your runtime beats 80.95 % of php submissions 37 | // Your memory usage beats 33.33 % of php submissions (16.1 MB) 38 | // @lc code=end 39 | 40 | -------------------------------------------------------------------------------- /每日1题202008/[0814]20.有效的括号.php: -------------------------------------------------------------------------------- 1 | '}', 21 | '[' => ']', 22 | '(' => ')', 23 | '?' => '?' 24 | ]; 25 | 26 | $left = ['{','[','(']; 27 | $stack = ["?"]; 28 | 29 | for ($i=0; $i < strlen($s); $i++) { 30 | if (in_array($s[$i], $left)) { // 如果是左括号,直接入栈 31 | $stack[] = $s[$i]; 32 | } elseif ($arr[array_pop($stack)] != $s[$i]) { // 如果不在右括号范围内,直接返回结果 33 | return false; 34 | } 35 | } 36 | 37 | return count($stack) == 1; 38 | } 39 | } 40 | 41 | // Accepted 42 | // 76/76 cases passed (8 ms) 43 | // Your runtime beats 79.31 % of php submissions 44 | // Your memory usage beats 70.97 % of php submissions (14.9 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /每日1题202008/[0815]546.移除盒子.php: -------------------------------------------------------------------------------- 1 | m_ = array_fill(0, $n, array_fill(0, $n, array_fill(0, $n, 0))); 22 | return $this->dfs($nums, 0, $n - 1, 0); 23 | } 24 | 25 | function dfs(&$nums, $l, $r, $k) { 26 | if ($l > $r) return 0; 27 | if ($this->m_[$l][$r][$k] > 0) return $this->m_[$l][$r][$k]; 28 | $this->m_[$l][$r][$k] = $this->dfs($nums, $l, $r - 1, 0) + ($k+1) * ($k+1); 29 | 30 | for ($i = $l; $i < $r; $i++) { 31 | if ($nums[$i] == $nums[$r]) { 32 | $this->m_[$l][$r][$k] = max($this->m_[$l][$r][$k], $this->dfs($nums, $l, $i, $k + 1) + $this->dfs($nums, $i + 1, $r - 1, 0)); 33 | } 34 | } 35 | 36 | return $this->m_[$l][$r][$k]; 37 | } 38 | } 39 | // @lc code=end 40 | 41 | -------------------------------------------------------------------------------- /每日1题202008/[0816]733.图像渲染.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Boolean 25 | */ 26 | function isBalanced($root) { 27 | 28 | // 自顶向下 29 | if ($root == null) { 30 | return true; 31 | } else { 32 | return abs($this->height($root->left) - $this->height($root->right)) <= 1 && $this->isBalanced($root->left) && $this->isBalanced($root->right); 33 | } 34 | } 35 | 36 | function height($root) { 37 | if ($root == null) { 38 | return 0; 39 | } else { 40 | return max($this->height($root->left), $this->height($root->right)) + 1; 41 | } 42 | } 43 | } 44 | 45 | // Accepted 46 | // 227/227 cases passed (24 ms) 47 | // Your runtime beats 19.4 % of php submissions 48 | // Your memory usage beats 69.7 % of php submissions (17 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /每日1题202008/[0821]111.二叉树的最小深度.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer 25 | */ 26 | function minDepth($root) { 27 | 28 | } 29 | } 30 | // @lc code=end 31 | 32 | -------------------------------------------------------------------------------- /每日1题202008/[0822]679.24-点游戏.php: -------------------------------------------------------------------------------- 1 | buildGraph($tickets, $g); // 构建图 22 | 23 | $stack = []; 24 | $this->dfs($g, $stack, "JFK"); // 深度优先搜索 25 | 26 | return array_reverse($stack); 27 | } 28 | 29 | function buildGraph($tickets, &$g) { 30 | 31 | foreach ($tickets as $ticket) { 32 | $from = $ticket[0]; 33 | $to = $ticket[1]; 34 | 35 | if (!array_key_exists($from, $g)) { 36 | $g[$from] = []; 37 | } 38 | 39 | $g[$from][] = $to; 40 | } 41 | 42 | foreach ($g as $from => $tos) { 43 | sort($g[$from]); 44 | } 45 | } 46 | 47 | function dfs(&$g, &$stack, $from) { 48 | 49 | while (!empty($g[$from])) { 50 | $to = array_shift($g[$from]); 51 | $this->dfs($g, $stack, $to); // 前一个目的地作为新的起点 52 | } 53 | 54 | array_push($stack, $from); 55 | } 56 | } 57 | 58 | // Accepted 59 | // 80/80 cases passed (32 ms) 60 | // Your runtime beats 100 % of php submissions 61 | // Your memory usage beats 100 % of php submissions (15.3 MB) 62 | // @lc code=end 63 | 64 | -------------------------------------------------------------------------------- /每日1题202008/[0828]657.机器人能否返回原点.php: -------------------------------------------------------------------------------- 1 | num = 0; 23 | $this->visited = array_fill(0, $n, 0); 24 | $this->dfs($rooms, 0); 25 | 26 | return $n == $this->num; // 是否全部访问过 27 | } 28 | 29 | function dfs($rooms, $x) { 30 | $this->visited[$x] = 1; 31 | $this->num++; 32 | 33 | foreach ($rooms[$x] as $key) { 34 | if (!$this->visited[$key]) { // 如果没有访问过,下探至下一层 35 | $this->dfs($rooms, $key); 36 | } 37 | } 38 | } 39 | } 40 | 41 | // Accepted 42 | // 67/67 cases passed (32 ms) 43 | // Your runtime beats 42.86 % of php submissions 44 | // Your memory usage beats 100 % of php submissions (16.3 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /每日1题202009/README.md: -------------------------------------------------------------------------------- 1 | # leetcode 每日1题 -------------------------------------------------------------------------------- /每日1题202009/[0901]486.预测赢家_记忆化递归.php: -------------------------------------------------------------------------------- 1 | size = count($nums); 26 | // 存储重叠的子问题解 27 | $this->m = array_fill(0, $this->size * $this->size, 0); 28 | 29 | // 判断玩家1 - 玩家2是否大于等于0 30 | return $this->getScore($nums, 0, count($nums) - 1) >= 0; 31 | } 32 | 33 | function getScore($nums, $l, $r) { 34 | 35 | if ($l == $r) return $nums[$l]; 36 | 37 | $k = $l * $this->size + $r; 38 | if ($this->m[$k] > 0) return $this->m[$k]; 39 | 40 | // 求两种情况下的最优解 41 | // 玩家1选择左端点L,那么玩家2的可选择区间则是[L + 1, R] 42 | // 玩家1选择右端点R,那么玩家2的可选择区间则是[L, R - 1] 43 | $this->m[$k] = max( 44 | $nums[$l] - $this->getScore($nums, $l + 1, $r), 45 | $nums[$r] - $this->getScore($nums, $l, $r - 1) 46 | ); 47 | 48 | return $this->m[$k]; 49 | } 50 | } 51 | 52 | // Accepted 53 | // 62/62 cases passed (12 ms) 54 | // Your runtime beats 100 % of php submissions 55 | // Your memory usage beats 100 % of php submissions (14.9 MB) 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /每日1题202009/[0901]486.预测赢家_递归.php: -------------------------------------------------------------------------------- 1 | getScore($nums, 0, count($nums) - 1) >= 0; 21 | } 22 | 23 | function getScore($nums, $l, $r) { 24 | 25 | if ($l == $r) return $nums[$l]; 26 | 27 | // 求两种情况下的最优解 28 | // 玩家1选择左端点L,那么玩家2的可选择区间则是[L + 1, R] 29 | // 玩家1选择右端点R,那么玩家2的可选择区间则是[L, R - 1] 30 | return max( 31 | $nums[$l] - $this->getScore($nums, $l + 1, $r), 32 | $nums[$r] - $this->getScore($nums, $l, $r - 1) 33 | ); 34 | } 35 | } 36 | 37 | // Accepted 38 | // 62/62 cases passed (1340 ms) 39 | // Your runtime beats 100 % of php submissions 40 | // Your memory usage beats 100 % of php submissions (14.9 MB) 41 | // 效率较低 42 | // @lc code=end 43 | 44 | -------------------------------------------------------------------------------- /每日1题202009/[0904]257.二叉树的所有路径.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | protected $arr; 23 | /** 24 | * @param TreeNode $root 25 | * @return String[] 26 | */ 27 | function binaryTreePaths($root) { 28 | 29 | if ($root == null) return []; 30 | 31 | $this->dfs($root, ""); 32 | return $this->arr; 33 | } 34 | 35 | function dfs($root, $tmp) { 36 | 37 | if ($root != null) { 38 | $tmp = ($tmp == "") ? (string)$root->val : $tmp."->".$root->val; 39 | if ($root->left == null && $root->right == null) { 40 | $this->arr[] = $tmp; 41 | } else { 42 | $this->dfs($root->left, $tmp); 43 | $this->dfs($root->right, $tmp); 44 | } 45 | } 46 | } 47 | } 48 | 49 | // Accepted 50 | // 209/209 cases passed (12 ms) 51 | // Your runtime beats 17.14 % of php submissions 52 | // Your memory usage beats 7.14 % of php submissions (15.1 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202009/[0906]107.二叉树的层次遍历-ii.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer[][] 25 | */ 26 | function levelOrderBottom($root) { 27 | 28 | // 使用辅助队列,先进先出 29 | $queue = []; 30 | array_push($queue, [0, $root]); 31 | $res = []; 32 | 33 | while (!empty($queue)) { 34 | $cur = array_shift($queue); 35 | 36 | if ($cur[1] == null) continue; 37 | $res[$cur[0]][] = $cur[1]->val; 38 | 39 | array_push($queue, [$cur[0] + 1, $cur[1]->left]); 40 | array_push($queue, [$cur[0] + 1, $cur[1]->right]); 41 | } 42 | 43 | return array_reverse($res); 44 | } 45 | } 46 | 47 | // Accepted 48 | // 34/34 cases passed (8 ms) 49 | // Your runtime beats 87.5 % of php submissions 50 | // Your memory usage beats 39.03 % of php submissions (15.7 MB) 51 | // @lc code=end 52 | 53 | -------------------------------------------------------------------------------- /每日1题202009/[0907]347.前-k-个高频元素.php: -------------------------------------------------------------------------------- 1 | $val) { 33 | 34 | $new_res[] = $key; 35 | if(++$i == $k) break; // 取前k个 36 | } 37 | 38 | return $new_res; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 21/21 cases passed (36 ms) 44 | // Your runtime beats 72.04 % of php submissions 45 | // Your memory usage beats 89.74 % of php submissions (19.7 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /每日1题202009/[0908]77.组合.php: -------------------------------------------------------------------------------- 1 | n = $n; 25 | $this->k = $k; 26 | $res = []; 27 | $tmp = []; 28 | $this->helper(1, $tmp, $res); 29 | return $res; 30 | } 31 | 32 | function helper($start, &$tmp, &$res) { 33 | 34 | if(count($tmp) == $this->k){ 35 | $res[] = $tmp; 36 | return true; 37 | } 38 | 39 | // 搜索起点的上界 + 接下来要选择的元素个数 - 1 = n 40 | // 搜索起点的上界 = n - (k - path.size()) + 1 41 | for ($i=$start; $i <= ($this->n - ($this->k - count($tmp)) + 1); $i++) { 42 | $tmp[] = $i; 43 | $this->helper($i + 1, $tmp, $res); 44 | array_pop($tmp); 45 | } 46 | } 47 | } 48 | 49 | // Accepted 50 | // 27/27 cases passed (24 ms) 51 | // Your runtime beats 97.6 % of php submissions 52 | // Your memory usage beats 59.54 % of php submissions (19.7 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202009/[0909]39.组合总和.php: -------------------------------------------------------------------------------- 1 | results = []; 24 | $result = []; 25 | sort($candidates); 26 | $this->dfs($result, $candidates, $target, 0); 27 | return $this->results; 28 | } 29 | 30 | function dfs($result, $candidates, $target, $level) { 31 | 32 | if ($target == 0) { 33 | $this->results[] = $result; 34 | return true; 35 | } 36 | 37 | for ($i=$level; $i < count($candidates) && $target - $candidates[$i] >= 0; $i++) { 38 | array_push($result, $candidates[$i]); 39 | $this->dfs($result, $candidates, $target - $candidates[$i], $i); 40 | array_pop($result); 41 | } 42 | } 43 | } 44 | // @lc code=end 45 | 46 | -------------------------------------------------------------------------------- /每日1题202009/[0910]40.组合总和-ii.php: -------------------------------------------------------------------------------- 1 | results = []; 24 | $result = []; 25 | sort($candidates); // 排序方便剪枝 26 | $this->dfs($result, $candidates, $target, 0); 27 | return $this->results; 28 | } 29 | 30 | function dfs($result, $candidates, $target, $level) { 31 | 32 | if ($target == 0) { 33 | $this->results[] = $result; 34 | return true; 35 | } 36 | 37 | for ($i=$level; $i < count($candidates); $i++) { 38 | 39 | if ($candidates[$i] > $target) return true; // 剪枝 40 | if ($i > $level && $candidates[$i] == $candidates[$i - 1]) continue; // 大于 level 证明不是第一个数,可以对比前一个数,相同则跳过 41 | 42 | array_push($result, $candidates[$i]); 43 | $this->dfs($result, $candidates, $target - $candidates[$i], $i + 1); 44 | array_pop($result); 45 | } 46 | } 47 | } 48 | 49 | // Accepted 50 | // 172/172 cases passed (8 ms) 51 | // Your runtime beats 100 % of php submissions 52 | // Your memory usage beats 33.33 % of php submissions (14.9 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202009/[0914]94.二叉树的中序遍历.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer[] 25 | */ 26 | function inorderTraversal($root) { 27 | 28 | $visited = 1; 29 | $not_visited = 0; 30 | $stack[] = [$not_visited, $root]; 31 | $res = []; 32 | 33 | while (!empty($stack)) { 34 | $cur = array_pop($stack); 35 | 36 | if ($cur[1] == null) continue; 37 | 38 | if ($cur[0] == 0) { 39 | $stack[] = [$not_visited, $cur[1]->right]; 40 | $stack[] = [$visited, $cur[1]]; 41 | $stack[] = [$not_visited, $cur[1]->left]; 42 | } else { 43 | $res[] = $cur[1]->val; 44 | } 45 | } 46 | 47 | return $res; 48 | } 49 | } 50 | 51 | // Accepted 52 | // 68/68 cases passed (8 ms) 53 | // Your runtime beats 61.32 % of php submissions 54 | // Your memory usage beats 33.62 % of php submissions (15 MB) 55 | // @lc code=end 56 | 57 | -------------------------------------------------------------------------------- /每日1题202009/[0914_1]144.二叉树的前序遍历.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer[] 25 | */ 26 | function preorderTraversal($root) { 27 | 28 | // 与中序遍历解法类似,调整了入栈顺序 29 | $visited = 1; 30 | $not_visited = 0; 31 | $stack[] = [$not_visited, $root]; 32 | $res = []; 33 | 34 | while (!empty($stack)) { 35 | $cur = array_pop($stack); 36 | 37 | if ($cur[1] == null) continue; 38 | 39 | if ($cur[0] == 0) { 40 | $stack[] = [$not_visited, $cur[1]->right]; 41 | $stack[] = [$not_visited, $cur[1]->left]; 42 | $stack[] = [$visited, $cur[1]]; 43 | } else { 44 | $res[] = $cur[1]->val; 45 | } 46 | } 47 | 48 | return $res; 49 | } 50 | } 51 | 52 | // Accepted 53 | // 68/68 cases passed (4 ms) 54 | // Your runtime beats 96.91 % of php submissions 55 | // Your memory usage beats 56.43 % of php submissions (14.9 MB) 56 | // @lc code=end 57 | 58 | -------------------------------------------------------------------------------- /每日1题202009/[0914_2]145.二叉树的后序遍历.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->left = $left; 20 | * $this->right = $right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | 26 | /** 27 | * @param TreeNode $root 28 | * @return Integer[] 29 | */ 30 | function postorderTraversal($root) { 31 | 32 | // 与中序遍历解法类似,调整了入栈顺序 33 | $visited = 1; 34 | $not_visited = 0; 35 | $stack[] = [$not_visited, $root]; 36 | $res = []; 37 | 38 | while (!empty($stack)) { 39 | $cur = array_pop($stack); 40 | 41 | if ($cur[1] == null) continue; 42 | 43 | if ($cur[0] == 0) { 44 | $stack[] = [$visited, $cur[1]]; 45 | $stack[] = [$not_visited, $cur[1]->right]; 46 | $stack[] = [$not_visited, $cur[1]->left]; 47 | } else { 48 | $res[] = $cur[1]->val; 49 | } 50 | } 51 | 52 | return $res; 53 | } 54 | } 55 | 56 | // Accepted 57 | // 68/68 cases passed (8 ms) 58 | // Your runtime beats 69.84 % of php submissions 59 | // Your memory usage beats 26.41 % of php submissions (15 MB) 60 | // @lc code=end 61 | 62 | -------------------------------------------------------------------------------- /每日1题202009/[0916]226.翻转二叉树.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return TreeNode 25 | */ 26 | function invertTree($root) { 27 | 28 | if ($root == null) return null; 29 | 30 | $right = $this->invertTree($root->right); 31 | $left = $this->invertTree($root->left); 32 | 33 | $root->right = $left; 34 | $root->left = $right; 35 | 36 | return $root; 37 | } 38 | } 39 | 40 | // Accepted 41 | // 68/68 cases passed (8 ms) 42 | // Your runtime beats 66.19 % of php submissions 43 | // Your memory usage beats 92.65 % of php submissions (14.4 MB) 44 | // @lc code=end 45 | 46 | -------------------------------------------------------------------------------- /每日1题202009/[0918]47.全排列-ii.php: -------------------------------------------------------------------------------- 1 | len = count($nums); 25 | $this->res = []; 26 | sort($nums); 27 | $tmp = []; 28 | $visited = []; 29 | 30 | $this->backtracking($tmp, $visited, $nums); 31 | return $this->res; 32 | } 33 | 34 | function backtracking(&$tmp, &$visited, $nums) { 35 | 36 | if (count($tmp) == $this->len) { 37 | $this->res[] = $tmp; 38 | return true; 39 | } 40 | 41 | for ($i=0; $i < $this->len; $i++) { 42 | 43 | if ($visited[$i] == "1") continue; 44 | 45 | // 剪枝 46 | if ($i > 0 && $nums[$i] == $nums[$i - 1] && !$visited[$i - 1]) continue; 47 | 48 | // 记录状态 49 | $visited[$i] = "1"; 50 | $tmp[] = $nums[$i]; 51 | 52 | $this->backtracking($tmp, $visited, $nums); 53 | 54 | // 恢复回溯前的状态 55 | $visited[$i] = "0"; 56 | array_pop($tmp); 57 | } 58 | } 59 | } 60 | 61 | // Accepted 62 | // 30/30 cases passed (16 ms) 63 | // Your runtime beats 91.11 % of php submissions 64 | // Your memory usage beats 21.87 % of php submissions (15.2 MB) 65 | // @lc code=end 66 | 67 | -------------------------------------------------------------------------------- /每日1题202009/[0919]404.左叶子之和.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $root 24 | * @return Integer 25 | */ 26 | function sumOfLeftLeaves($root) { 27 | 28 | if ($root == null) return 0; 29 | 30 | $res = 0; 31 | $this->dfs($root, $res); 32 | return $res; 33 | } 34 | 35 | function dfs($root, &$res) { 36 | 37 | if ($root->left != null) { 38 | $res += $this->isLeafNode($root->left) ? $root->left->val : $this->dfs($root->left, $res); 39 | } 40 | 41 | if ($root->right != null && !$this->isLeafNode($root->right)) { 42 | $res += $this->dfs($root->right, $res); 43 | } 44 | } 45 | 46 | // 判断是否叶子节点 47 | function isLeafNode($node) { 48 | 49 | if ($node->left == null && $node->right == null) { 50 | return true; 51 | } 52 | 53 | return false; 54 | } 55 | } 56 | 57 | // Accepted 58 | // 102/102 cases passed (8 ms) 59 | // Your runtime beats 67.74 % of php submissions 60 | // Your memory usage beats 18.18 % of php submissions (15.2 MB) 61 | // @lc code=end 62 | 63 | -------------------------------------------------------------------------------- /每日1题202009/[0920]78.子集.php: -------------------------------------------------------------------------------- 1 | res = []; 21 | 22 | // 控制个数,0到最大递增 23 | for ($i=0; $i <= count($nums); $i++) { 24 | $this->backtracking(0, $i, [], $nums); 25 | } 26 | 27 | return $this->res; 28 | } 29 | 30 | function backtracking($first, $k, $cur, $nums) { 31 | 32 | if (count($cur) == $k) { 33 | $this->res[] = $cur; 34 | return true; 35 | } 36 | 37 | for ($i=$first; $i < count($nums); $i++) { 38 | $cur[] = $nums[$i]; 39 | $this->backtracking($i + 1, $k, $cur, $nums); 40 | array_pop($cur); 41 | } 42 | } 43 | } 44 | 45 | // Accepted 46 | // 10/10 cases passed (12 ms) 47 | // Your runtime beats 37.72 % of php submissions 48 | // Your memory usage beats 34.43 % of php submissions (15.1 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /每日1题202009/[0921]538.把二叉搜索树转换为累加树.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | public $sum; 23 | 24 | /** 25 | * @param TreeNode $root 26 | * @return TreeNode 27 | */ 28 | function convertBST($root) { 29 | 30 | // 中序遍历是递增序列,反向中序就是递减序列,最小的值就能累加到前面大于子集的数 31 | if ($root != null) { 32 | $this->convertBST($root->right); 33 | $this->sum += $root->val; 34 | $root->val = $this->sum; 35 | $this->convertBST($root->left); 36 | } 37 | 38 | return $root; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 212/212 cases passed (28 ms) 44 | // Your runtime beats 69.23 % of php submissions 45 | // Your memory usage beats 8.82 % of php submissions (18.3 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /每日1题202009/[0923]617.合并二叉树.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | /** 23 | * @param TreeNode $t1 24 | * @param TreeNode $t2 25 | * @return TreeNode 26 | */ 27 | function mergeTrees($t1, $t2) { 28 | 29 | if ($t1 == null) return $t2; 30 | if ($t2 == null) return $t1; 31 | 32 | $root = $t1; 33 | $root->val += $t2->val; 34 | 35 | $root->left = $this->mergeTrees($t1->left, $t2->left); 36 | $root->right = $this->mergeTrees($t1->right, $t2->right); 37 | 38 | return $root; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 183/183 cases passed (24 ms) 44 | // Your runtime beats 89.86 % of php submissions 45 | // Your memory usage beats 81.58 % of php submissions (15.2 MB) 46 | // @lc code=end -------------------------------------------------------------------------------- /每日1题202009/[0927]235.二叉搜索树的最近公共祖先.php: -------------------------------------------------------------------------------- 1 | val = $value; } 19 | * } 20 | */ 21 | 22 | class Solution { 23 | /** 24 | * @param TreeNode $root 25 | * @param TreeNode $p 26 | * @param TreeNode $q 27 | * @return TreeNode 28 | */ 29 | function lowestCommonAncestor($root, $p, $q) { 30 | // 二叉搜索树的特征,左子树节点值 <= 中间节点值 <= 右子树节点值 31 | 32 | // 如果两者都小于根节点的值,那么搜索它的左子树 33 | if ($p->val < $root->val && $q->val < $root->val) { 34 | return $this->lowestCommonAncestor($root->left, $p, $q); 35 | } 36 | 37 | // 如果两者都大于根节点的值,那么搜索它的右子树 38 | if ($p->val > $root->val && $q->val > $root->val) { 39 | return $this->lowestCommonAncestor($root->right, $p, $q); 40 | } 41 | 42 | // 一大一小、一大于一相等、一小于一相等,那么返回当前根节点 43 | return $root; 44 | } 45 | } 46 | 47 | // Accepted 48 | // 27/27 cases passed (32 ms) 49 | // Your runtime beats 97.62 % of php submissions 50 | // Your memory usage beats 32.61 % of php submissions (18.1 MB) 51 | // @lc code=end 52 | 53 | -------------------------------------------------------------------------------- /每日1题202009/[0930]701.二叉搜索树中的插入操作.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->left = $left; 20 | * $this->right = $right; 21 | * } 22 | * } 23 | */ 24 | class Solution { 25 | 26 | /** 27 | * @param TreeNode $root 28 | * @param Integer $val 29 | * @return TreeNode 30 | */ 31 | function insertIntoBST($root, $val) { 32 | 33 | if ($root == null) return new TreeNode($val); 34 | 35 | $this->helper($root, $val); 36 | return $root; 37 | } 38 | 39 | function helper(&$root, $val) { 40 | 41 | if ($root->val > $val) { 42 | if ($root->left == null) { 43 | $root->left = new TreeNode($val); 44 | return true; 45 | } 46 | $this->helper($root->left, $val); 47 | } 48 | 49 | if ($root->val < $val) { 50 | if ($root->right == null) { 51 | $root->right = new TreeNode($val); 52 | return true; 53 | } 54 | $this->helper($root->right, $val); 55 | } 56 | 57 | } 58 | } 59 | 60 | // Accepted 61 | // 35/35 cases passed (44 ms) 62 | // Your runtime beats 88 % of php submissions 63 | // Your memory usage beats 50 % of php submissions (16.8 MB) 64 | // @lc code=end 65 | 66 | -------------------------------------------------------------------------------- /每日1题202010/README.md: -------------------------------------------------------------------------------- 1 | # leetcode 每日1题 -------------------------------------------------------------------------------- /每日1题202010/[1003]1.两数之和.php: -------------------------------------------------------------------------------- 1 | $num) { 22 | if (array_key_exists($target - $num, $visited)) { 23 | if ($nums[$visited[$target - $num]] + $num === $target) { 24 | return [$visited[$target - $num], $k]; 25 | } 26 | } 27 | 28 | $visited[$num] = $k; 29 | } 30 | } 31 | } 32 | 33 | // Accepted 34 | // 29/29 cases passed (16 ms) 35 | // Your runtime beats 80.91 % of php submissions 36 | // Your memory usage beats 32.61 % of php submissions (15.6 MB) 37 | // @lc code=end 38 | 39 | -------------------------------------------------------------------------------- /每日1题202010/[1007]75.颜色分类_单向遍历.php: -------------------------------------------------------------------------------- 1 | val = $val; } 17 | * } 18 | */ 19 | 20 | class Solution { 21 | /** 22 | * @param ListNode $head 23 | * @return Boolean 24 | */ 25 | function hasCycle($head) { 26 | $visited = []; 27 | 28 | while ($head != null) { 29 | if (in_array($head, $visited)) { // listNode 也可以这样查找 30 | return true; 31 | } 32 | $visited[] = $head; 33 | $head = $head->next; 34 | } 35 | return false; 36 | } 37 | } 38 | 39 | // Accepted 40 | // 17/17 cases passed (2064 ms) 41 | // Your runtime beats 10.94 % of php submissions 42 | // Your memory usage beats 37.08 % of php submissions (17.3 MB) 43 | // @lc code=end 44 | 45 | -------------------------------------------------------------------------------- /每日1题202010/[1009]141.环形链表_快慢指针.php: -------------------------------------------------------------------------------- 1 | val = $val; } 17 | * } 18 | */ 19 | 20 | class Solution { 21 | /** 22 | * @param ListNode $head 23 | * @return Boolean 24 | */ 25 | function hasCycle($head) { 26 | if ($head == null || $head->next == null) { 27 | return false; 28 | } 29 | 30 | // 快指针走两步,慢指针走一步,如果能够相遇则该链表有环 31 | $slow = $head; 32 | $fast = $head->next->next; 33 | while ($fast != $slow) { 34 | if ($slow == null || $fast == null) { 35 | return false; 36 | } 37 | $slow = $slow->next; 38 | $fast = $fast->next->next; 39 | } 40 | 41 | return true; 42 | } 43 | } 44 | 45 | // Accepted 46 | // 17/17 cases passed (16 ms) 47 | // Your runtime beats 70.94 % of php submissions 48 | // Your memory usage beats 65.56 % of php submissions (17.2 MB) 49 | // @lc code=end 50 | 51 | -------------------------------------------------------------------------------- /每日1题202010/[1016]977.有序数组的平方_双指针.php: -------------------------------------------------------------------------------- 1 | $A[$j] * $A[$j]) { 24 | $ans[$pos] = $A[$i] * $A[$i]; 25 | $i++; 26 | } else { 27 | $ans[$pos] = $A[$j] * $A[$j]; 28 | $j--; 29 | } 30 | $pos--; 31 | } 32 | return $ans; 33 | } 34 | } 35 | 36 | // Accepted 37 | // 132/132 cases passed (76 ms) 38 | // Your runtime beats 97.06 % of php submissions 39 | // Your memory usage beats 9.52 % of php submissions (18 MB) 40 | // @lc code=end 41 | 42 | -------------------------------------------------------------------------------- /每日1题202010/[1016]977.有序数组的平方_常规解法.php: -------------------------------------------------------------------------------- 1 | $v) { 22 | $ans[$k] = $v * $v; 23 | } 24 | 25 | sort($ans); 26 | return $ans; 27 | } 28 | } 29 | 30 | // Accepted 31 | // 132/132 cases passed (72 ms) 32 | // Your runtime beats 100 % of php submissions 33 | // Your memory usage beats 9.52 % of php submissions (17.9 MB) 34 | // @lc code=end 35 | 36 | -------------------------------------------------------------------------------- /每日1题202010/[1018]19.删除链表的倒数第n个节点.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->next = $next; 20 | * } 21 | * } 22 | */ 23 | class Solution { 24 | 25 | /** 26 | * @param ListNode $head 27 | * @param Integer $n 28 | * @return ListNode 29 | */ 30 | function removeNthFromEnd($head, $n) { 31 | if (!$head) return null; 32 | 33 | $nodes = []; 34 | $cur = $head; 35 | while ($cur) { 36 | $nodes[] = $cur; 37 | $cur = $cur->next; 38 | } 39 | 40 | // 链表长度 == n 41 | $size = count($nodes); 42 | if ($n == $size) return $head->next; 43 | 44 | // 找到需要删除的结点 45 | $node_to_remove = $nodes[$size - $n]; 46 | // 找到被删除的结点的前一个节点 47 | $parent = $nodes[$size - $n - 1]; 48 | // 将 next 指针指向被删除的结点的 next 指针 49 | $parent->next = $node_to_remove->next; 50 | 51 | return $head; 52 | } 53 | } 54 | 55 | // Accepted 56 | // 208/208 cases passed (8 ms) 57 | // Your runtime beats 71.79 % of php submissions 58 | // Your memory usage beats 30.52 % of php submissions (15 MB) 59 | // @lc code=end 60 | 61 | -------------------------------------------------------------------------------- /每日1题202010/[1018]19.删除链表的倒数第n个节点_双指针.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->next = $next; 20 | * } 21 | * } 22 | */ 23 | class Solution { 24 | 25 | /** 26 | * @param ListNode $head 27 | * @param Integer $n 28 | * @return ListNode 29 | */ 30 | function removeNthFromEnd($head, $n) { 31 | // 不使用数组存放节点的做法 32 | $l = 0; 33 | $cur = $head; 34 | while ($cur) { 35 | $l++; 36 | $cur = $cur->next; 37 | } 38 | 39 | if ($n == $l) { 40 | return $head->next; 41 | } 42 | 43 | $l = $l - $n; 44 | $cur = $head; 45 | while (--$l) { 46 | $cur = $cur->next; // 需要删除结点的前一个 47 | } 48 | $node = $cur->next; // 需要删除的结点 49 | $cur->next = $node->next; // 需要删除的结点的后一个 50 | return $head; 51 | } 52 | } 53 | 54 | // Accepted 55 | // 208/208 cases passed (12 ms) 56 | // Your runtime beats 27.11 % of php submissions 57 | // Your memory usage beats 16.07 % of php submissions (15.3 MB) 58 | // @lc code=end 59 | 60 | -------------------------------------------------------------------------------- /每日1题202010/[1019]844.比较含退格的字符串.php: -------------------------------------------------------------------------------- 1 | helper($S) == $this->helper($T); 20 | } 21 | 22 | function helper($str) { 23 | if (strlen($str) == 1) return $str; 24 | 25 | $o = []; 26 | for ($i=0; $i < strlen($str); $i++) { 27 | $c = $str[$i]; 28 | if ($c == '#') { 29 | if (!empty($o)) { 30 | array_pop($o); 31 | } 32 | } else { 33 | array_push($o, $c); 34 | } 35 | } 36 | 37 | return implode("", $o); 38 | } 39 | } 40 | 41 | // Accepted 42 | // 110/110 cases passed (4 ms) 43 | // Your runtime beats 97.65 % of php submissions 44 | // Your memory usage beats 71.6 % of php submissions (15.1 MB) 45 | // @lc code=end 46 | 47 | -------------------------------------------------------------------------------- /每日1题202010/[1020]143.重排链表_线性表存储.php: -------------------------------------------------------------------------------- 1 | val = $val; 19 | * $this->next = $next; 20 | * } 21 | * } 22 | */ 23 | class Solution { 24 | 25 | /** 26 | * @param ListNode $head 27 | * @return NULL 28 | */ 29 | function reorderList($head) { 30 | if ($head == null) { 31 | return null; 32 | } 33 | 34 | $list = []; 35 | while ($head != null) { 36 | $list[] = $head; 37 | $head = $head->next; 38 | } 39 | 40 | $i = 0; 41 | $j = count($list) - 1; 42 | while ($i < $j) { 43 | $list[$i]->next = $list[$j]; // 例如: L(0)->next = L(N) 44 | $i++; 45 | // 偶数个节点的情况,会提前相遇 46 | if ($i == $j) { 47 | break; 48 | } 49 | $list[$j]->next = $list[$i]; // i++,此时 L(0)->L(N)->next = L(1) 50 | $j--; 51 | } 52 | 53 | $list[$i]->next = null; 54 | } 55 | } 56 | 57 | // Accepted 58 | // 13/13 cases passed (44 ms) 59 | // Your runtime beats 14.14 % of php submissions 60 | // Your memory usage beats 41.05 % of php submissions (25.1 MB) 61 | // @lc code=end 62 | 63 | -------------------------------------------------------------------------------- /每日1题202010/[1021]925.长按键入.php: -------------------------------------------------------------------------------- 1 | 0 && $typed_arr[$j] == $typed_arr[$j - 1]) { // 重复按的情况,type右移一位 33 | $j++; 34 | } else { 35 | return false; 36 | } 37 | } 38 | 39 | // type 还没走完,剩下的都和前面一样 40 | while ($j < $t_len && $typed_arr[$j] == $typed_arr[$j - 1]) { 41 | $j++; 42 | } 43 | 44 | // 两者都走到最后一位,才算匹配 45 | return $i == $n_len && $j == $t_len; 46 | } 47 | } 48 | 49 | // Accepted 50 | // 84/84 cases passed (4 ms) 51 | // Your runtime beats 96.32 % of php submissions 52 | // Your memory usage beats 77.95 % of php submissions (15.2 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202010/[1022]763.划分字母区间.php: -------------------------------------------------------------------------------- 1 | val = $val; } 17 | * } 18 | */ 19 | class Solution { 20 | 21 | /** 22 | * @param ListNode $head 23 | * @return Boolean 24 | */ 25 | function isPalindrome($head) { 26 | 27 | if ($head == null) return true; 28 | 29 | $tmp = []; 30 | while ($head != null) { 31 | $tmp[] = $head->val; 32 | $head = $head->next; 33 | } 34 | 35 | $i = 0; 36 | $j = count($tmp) - 1; 37 | while ($i < $j) { 38 | if ($tmp[$i] != $tmp[$j]) { 39 | return false; 40 | } 41 | $i++; 42 | $j--; 43 | } 44 | 45 | return true; 46 | } 47 | } 48 | 49 | // Accepted 50 | // 26/26 cases passed (24 ms) 51 | // Your runtime beats 87.57 % of php submissions 52 | // Your memory usage beats 49.16 % of php submissions (27.4 MB) 53 | // @lc code=end 54 | 55 | -------------------------------------------------------------------------------- /每日1题202010/[1028]1207.独一无二的出现次数.php: -------------------------------------------------------------------------------- 1 | $v) { 21 | if (!array_key_exists($v, $tmp)) { // 统计次数 22 | $tmp[$v] = 1; 23 | } else { 24 | $tmp[$v]++; 25 | } 26 | } 27 | 28 | sort($tmp); // 排序 29 | $prev = 0; 30 | foreach ($tmp as $k1 => $v1) { 31 | if ($prev > 0 && $prev == $v1) { // 出现相同的返回 false 32 | return false; 33 | } else { 34 | $prev = $v1; 35 | } 36 | } 37 | 38 | return true; 39 | } 40 | } 41 | 42 | // Accepted 43 | // 63/63 cases passed (8 ms) 44 | // Your runtime beats 69.57 % of php submissions 45 | // Your memory usage beats 9.52 % of php submissions (15.5 MB) 46 | // @lc code=end 47 | 48 | -------------------------------------------------------------------------------- /每日1题202010/[1029]129.求根到叶子节点数字之和.php: -------------------------------------------------------------------------------- 1 | val = $value; } 18 | * } 19 | */ 20 | class Solution { 21 | 22 | protected $ans; 23 | 24 | /** 25 | * @param TreeNode $root 26 | * @return Integer 27 | */ 28 | function sumNumbers($root) { 29 | 30 | if ($root == null) return 0; 31 | 32 | $this->ans = 0; 33 | $this->helper($root, 0); 34 | return $this->ans; 35 | } 36 | 37 | function helper($t, $num) { 38 | 39 | if ($t == null) return true; 40 | 41 | $num = 10 * $num + $t->val; // 1->2->3 == 123 42 | if ($t->left == null && $t->right == null) { // 叶子节点时退出 43 | $this->ans += $num; 44 | } else { 45 | $this->helper($t->left, $num); 46 | $this->helper($t->right, $num); 47 | } 48 | } 49 | } 50 | 51 | // Accepted 52 | // 110/110 cases passed (8 ms) 53 | // Your runtime beats 64.71 % of php submissions 54 | // Your memory usage beats 25 % of php submissions (15.6 MB) 55 | // @lc code=end 56 | 57 | -------------------------------------------------------------------------------- /每日1题202010/[1030]463.岛屿的周长.php: -------------------------------------------------------------------------------- 1 | 0 && $grid[$i - 1][$j] == 1) ++$conn; // 如果上面有岛屿各子,记录一次相邻 31 | if ($j > 0 && $grid[$i][$j - 1] == 1) ++$conn; // 如果左边有岛屿各子,记录一次相邻 32 | } 33 | } 34 | } 35 | 36 | return $area*4 - $conn*2; // 每次相邻需要减去合并的两条边 37 | } 38 | } 39 | 40 | // Accepted 41 | // 5833/5833 cases passed (220 ms) 42 | // Your runtime beats 65.22 % of php submissions 43 | // Your memory usage beats 52.63 % of php submissions (17.3 MB) 44 | // @lc code=end 45 | 46 | -------------------------------------------------------------------------------- /每日1题202011/README.md: -------------------------------------------------------------------------------- 1 | # leetcode 每日1题 -------------------------------------------------------------------------------- /每日1题202011/[1102]349.两个数组的交集_暴力解法.php: -------------------------------------------------------------------------------- 1 | $n2) { 25 | $nums = array_unique($nums2); 26 | foreach ($nums as $v) { 27 | if (in_array($v, $nums1)) { 28 | $ans[] = $v; 29 | } 30 | } 31 | } else { 32 | $nums = array_unique($nums1); 33 | foreach ($nums as $v) { 34 | if (in_array($v, $nums2)) { 35 | $ans[] = $v; 36 | } 37 | } 38 | } 39 | 40 | return $ans; 41 | } 42 | } 43 | 44 | // Accepted 45 | // 60/60 cases passed (16 ms) 46 | // Your runtime beats 66.67 % of php submissions 47 | // Your memory usage beats 18.94 % of php submissions (15.3 MB) 48 | // @lc code=end 49 | 50 | -------------------------------------------------------------------------------- /每日1题202011/[1103]941.有效的山脉数组.php: -------------------------------------------------------------------------------- 1 | $A[$i]) { 27 | if ($large == 2) return false; // 上坡后下坡,不允许再次上坡 28 | if ($large == 1 && $less == 1) $large = 2; 29 | $large = 1; 30 | } elseif ($cur < $A[$i]) { 31 | if ($large == 1) return false; // 不允许出现凹型 32 | $less = 1; 33 | } else { 34 | return false; // 前后不可以出现相等 35 | } 36 | $cur = $A[$i]; 37 | } 38 | 39 | if ($large == 0 || $less == 0) return false; // 持续上坡、持续下坡都不符合条件 40 | return true; 41 | } 42 | } 43 | 44 | // Accepted 45 | // 51/51 cases passed (76 ms) 46 | // Your runtime beats 46.67 % of php submissions 47 | // Your memory usage beats 14.29 % of php submissions (16.5 MB) 48 | // @lc code=end 49 | 50 | --------------------------------------------------------------------------------