diff --git "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" index 6969c2e27e..fb3c1d4560 100644 --- "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" +++ "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" @@ -7,7 +7,7 @@ ## 1. 两数之和 -[力扣题目链接](https://leetcode-cn.com/problems/two-sum/) +[力扣题目链接](https://leetcode.cn/problems/two-sum/) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 diff --git "a/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" "b/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" index eaebb5ab4d..d53acf63e6 100644 --- "a/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" +++ "b/problems/0005.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円344円270円262円.md" @@ -8,7 +8,7 @@ # 5.最长回文子串 -[力扣题目链接](https://leetcode-cn.com/problems/longest-palindromic-substring/) +[力扣题目链接](https://leetcode.cn/problems/longest-palindromic-substring/) 给你一个字符串 s,找到 s 中最长的回文子串。 diff --git "a/problems/0015.344円270円211円346円225円260円344円271円213円345円222円214円.md" "b/problems/0015.344円270円211円346円225円260円344円271円213円345円222円214円.md" index 1764d244b6..4f1d711a7a 100644 --- "a/problems/0015.344円270円211円346円225円260円344円271円213円345円222円214円.md" +++ "b/problems/0015.344円270円211円346円225円260円344円271円213円345円222円214円.md" @@ -10,7 +10,7 @@ # 第15题. 三数之和 -[力扣题目链接](https://leetcode-cn.com/problems/3sum/) +[力扣题目链接](https://leetcode.cn/problems/3sum/) 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。 @@ -345,6 +345,76 @@ var threeSum = function(nums) { return res }; ``` + +解法二:nSum通用解法。递归 + +```js +/** + * nsum通用解法,支持2sum,3sum,4sum...等等 + * 时间复杂度分析: + * 1. n = 2时,时间复杂度O(NlogN),排序所消耗的时间。、 + * 2. n> 2时,时间复杂度为O(N^n-1),即N的n-1次方,至少是2次方,此时可省略排序所消耗的时间。举例:3sum为O(n^2),4sum为O(n^3) + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + // nsum通用解法核心方法 + function nSumTarget(nums, n, start, target) { + // 前提:nums要先排序好 + let res = []; + if (n === 2) { + res = towSumTarget(nums, start, target); + } else { + for (let i = start; i < nums.length; i++) { + // 递归求(n - 1)sum + let subRes = nSumTarget( + nums, + n - 1, + i + 1, + target - nums[i] + ); + for (let j = 0; j < subRes.length; j++) { + res.push([nums[i], ...subRes[j]]); + } + // 跳过相同元素 + while (nums[i] === nums[i + 1]) i++; + } + } + return res; + } + + function towSumTarget(nums, start, target) { + // 前提:nums要先排序好 + let res = []; + let len = nums.length; + let left = start; + let right = len - 1; + while (left < right) { + let sum = nums[left] + nums[right]; + if (sum < target) { + while (nums[left] === nums[left + 1]) left++; + left++; + } else if (sum> target) { + while (nums[right] === nums[right - 1]) right--; + right--; + } else { + // 相等 + res.push([nums[left], nums[right]]); + // 跳过相同元素 + while (nums[left] === nums[left + 1]) left++; + while (nums[right] === nums[right - 1]) right--; + left++; + right--; + } + } + return res; + } + nums.sort((a, b) => a - b); + // n = 3,此时求3sum之和 + return nSumTarget(nums, 3, 0, 0); +}; +``` + TypeScript: ```typescript diff --git "a/problems/0017.347円224円265円350円257円235円345円217円267円347円240円201円347円232円204円345円255円227円346円257円215円347円273円204円345円220円210円.md" "b/problems/0017.347円224円265円350円257円235円345円217円267円347円240円201円347円232円204円345円255円227円346円257円215円347円273円204円345円220円210円.md" index 9413656515..2b5cb978a2 100644 --- "a/problems/0017.347円224円265円350円257円235円345円217円267円347円240円201円347円232円204円345円255円227円346円257円215円347円273円204円345円220円210円.md" +++ "b/problems/0017.347円224円265円350円257円235円345円217円267円347円240円201円347円232円204円345円255円227円346円257円215円347円273円204円345円220円210円.md" @@ -7,7 +7,7 @@ # 17.电话号码的字母组合 -[力扣题目链接](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/) +[力扣题目链接](https://leetcode.cn/problems/letter-combinations-of-a-phone-number/) 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 diff --git "a/problems/0018.345円233円233円346円225円260円344円271円213円345円222円214円.md" "b/problems/0018.345円233円233円346円225円260円344円271円213円345円222円214円.md" index 6cbd40c288..2146a11469 100644 --- "a/problems/0018.345円233円233円346円225円260円344円271円213円345円222円214円.md" +++ "b/problems/0018.345円233円233円346円225円260円344円271円213円345円222円214円.md" @@ -10,7 +10,7 @@ # 第18题. 四数之和 -[力扣题目链接](https://leetcode-cn.com/problems/4sum/) +[力扣题目链接](https://leetcode.cn/problems/4sum/) 题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。 diff --git "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" index c36900bcde..3499ab9db4 100644 --- "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" +++ "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" @@ -9,7 +9,7 @@ ## 19.删除链表的倒数第N个节点 -[力扣题目链接](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/) +[力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/) 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 @@ -289,6 +289,30 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { return dummyHead.next } ``` + + +PHP: +```php +function removeNthFromEnd($head, $n) { + // 设置虚拟头节点 + $dummyHead = new ListNode(); + $dummyHead->next = $head; + + $slow = $fast = $dummyHead; + while($n-- && $fast != null){ + $fast = $fast->next; + } + // fast 再走一步,让 slow 指向删除节点的上一个节点 + $fast = $fast->next; + while ($fast != NULL) { + $fast = $fast->next; + $slow = $slow->next; + } + $slow->next = $slow->next->next; + return $dummyHead->next; + } +``` + Scala: ```scala object Solution { diff --git "a/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md" "b/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md" index a0df0d07fc..c3ff9d531c 100644 --- "a/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md" +++ "b/problems/0020.346円234円211円346円225円210円347円232円204円346円213円254円345円217円267円.md" @@ -10,7 +10,7 @@ # 20. 有效的括号 -[力扣题目链接](https://leetcode-cn.com/problems/valid-parentheses/) +[力扣题目链接](https://leetcode.cn/problems/valid-parentheses/) 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 @@ -400,6 +400,37 @@ bool isValid(char * s){ return !stackTop; } ``` + + +PHP: +```php +// https://www.php.net/manual/zh/class.splstack.php +class Solution +{ + function isValid($s){ + $stack = new SplStack(); + for ($i = 0; $i < strlen($s); $i++) { + if ($s[$i] == "(") { + $stack->push(')'); + } else if ($s[$i] == "{") { + $stack->push('}'); + } else if ($s[$i] == "[") { + $stack->push(']'); + // 2、遍历匹配过程中,发现栈内没有要匹配的字符 return false + // 3、遍历匹配过程中,栈已为空,没有匹配的字符了,说明右括号没有找到对应的左括号 return false + } else if ($stack->isEmpty() || $stack->top() != $s[$i]) { + return false; + } else {//$stack->top() == $s[$i] + $stack->pop(); + } + } + // 1、遍历完,但是栈不为空,说明有相应的括号没有被匹配,return false + return $stack->isEmpty(); + } +} +``` + + Scala: ```scala object Solution { @@ -422,5 +453,6 @@ object Solution { } } ``` + -----------------------
diff --git "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" index 2289c22912..e636bfff58 100644 --- "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" +++ "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" @@ -7,7 +7,7 @@ ## 24. 两两交换链表中的节点 -[力扣题目链接](https://leetcode-cn.com/problems/swap-nodes-in-pairs/) +[力扣题目链接](https://leetcode.cn/problems/swap-nodes-in-pairs/) 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 diff --git "a/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" "b/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" index f7142bad94..03c58b43ab 100644 --- "a/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" +++ "b/problems/0027.347円247円273円351円231円244円345円205円203円347円264円240円.md" @@ -7,7 +7,7 @@ ## 27. 移除元素 -[力扣题目链接](https://leetcode-cn.com/problems/remove-element/) +[力扣题目链接](https://leetcode.cn/problems/remove-element/) 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 diff --git "a/problems/0028.345円256円236円347円216円260円strStr.md" "b/problems/0028.345円256円236円347円216円260円strStr.md" index 1cdd5292a9..a95a52f814 100644 --- "a/problems/0028.345円256円236円347円216円260円strStr.md" +++ "b/problems/0028.345円256円236円347円216円260円strStr.md" @@ -9,7 +9,7 @@ # 28. 实现 strStr() -[力扣题目链接](https://leetcode-cn.com/problems/implement-strstr/) +[力扣题目链接](https://leetcode.cn/problems/implement-strstr/) 实现 strStr() 函数。 diff --git "a/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" "b/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" index 2219e24dba..bce8adef88 100644 --- "a/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" +++ "b/problems/0031.344円270円213円344円270円200円344円270円252円346円216円222円345円210円227円.md" @@ -9,7 +9,7 @@ # 31.下一个排列 -[力扣题目链接](https://leetcode-cn.com/problems/next-permutation/) +[力扣题目链接](https://leetcode.cn/problems/next-permutation/) 实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。 diff --git "a/problems/0035.346円220円234円347円264円242円346円217円222円345円205円245円344円275円215円347円275円256円.md" "b/problems/0035.346円220円234円347円264円242円346円217円222円345円205円245円344円275円215円347円275円256円.md" index 5cf44ded55..ed7c49b47f 100644 --- "a/problems/0035.346円220円234円347円264円242円346円217円222円345円205円245円344円275円215円347円275円256円.md" +++ "b/problems/0035.346円220円234円347円264円242円346円217円222円345円205円245円344円275円215円347円275円256円.md" @@ -9,7 +9,7 @@ # 35.搜索插入位置 -[力扣题目链接](https://leetcode-cn.com/problems/search-insert-position/) +[力扣题目链接](https://leetcode.cn/problems/search-insert-position/) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 diff --git "a/problems/0037.350円247円243円346円225円260円347円213円254円.md" "b/problems/0037.350円247円243円346円225円260円347円213円254円.md" index c1ac15af89..b074b98f34 100644 --- "a/problems/0037.350円247円243円346円225円260円347円213円254円.md" +++ "b/problems/0037.350円247円243円346円225円260円347円213円254円.md" @@ -9,7 +9,7 @@ # 37. 解数独 -[力扣题目链接](https://leetcode-cn.com/problems/sudoku-solver/) +[力扣题目链接](https://leetcode.cn/problems/sudoku-solver/) 编写一个程序,通过填充空格来解决数独问题。 diff --git "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" index e10a827f04..fef9b676aa 100644 --- "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" +++ "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" @@ -7,7 +7,7 @@ # 39. 组合总和 -[力扣题目链接](https://leetcode-cn.com/problems/combination-sum/) +[力扣题目链接](https://leetcode.cn/problems/combination-sum/) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 diff --git "a/problems/0040.347円273円204円345円220円210円346円200円273円345円222円214円II.md" "b/problems/0040.347円273円204円345円220円210円346円200円273円345円222円214円II.md" index 34ac64e68d..ee4371cd6b 100644 --- "a/problems/0040.347円273円204円345円220円210円346円200円273円345円222円214円II.md" +++ "b/problems/0040.347円273円204円345円220円210円346円200円273円345円222円214円II.md" @@ -9,7 +9,7 @@ # 40.组合总和II -[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-ii/) +[力扣题目链接](https://leetcode.cn/problems/combination-sum-ii/) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 diff --git "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" index 060c0b4578..4832423deb 100644 --- "a/problems/0042.346円216円245円351円233円250円346円260円264円.md" +++ "b/problems/0042.346円216円245円351円233円250円346円260円264円.md" @@ -9,7 +9,7 @@ # 42. 接雨水 -[力扣题目链接](https://leetcode-cn.com/problems/trapping-rain-water/) +[力扣题目链接](https://leetcode.cn/problems/trapping-rain-water/) 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 diff --git "a/problems/0045.350円267円263円350円267円203円346円270円270円346円210円217円II.md" "b/problems/0045.350円267円263円350円267円203円346円270円270円346円210円217円II.md" index 4e3ab24a83..9e61fbb88a 100644 --- "a/problems/0045.350円267円263円350円267円203円346円270円270円346円210円217円II.md" +++ "b/problems/0045.350円267円263円350円267円203円346円270円270円346円210円217円II.md" @@ -9,7 +9,7 @@ # 45.跳跃游戏II -[力扣题目链接](https://leetcode-cn.com/problems/jump-game-ii/) +[力扣题目链接](https://leetcode.cn/problems/jump-game-ii/) 给定一个非负整数数组,你最初位于数组的第一个位置。 diff --git "a/problems/0046.345円205円250円346円216円222円345円210円227円.md" "b/problems/0046.345円205円250円346円216円222円345円210円227円.md" index 6c55e8ef41..9f93564c02 100644 --- "a/problems/0046.345円205円250円346円216円222円345円210円227円.md" +++ "b/problems/0046.345円205円250円346円216円222円345円210円227円.md" @@ -7,7 +7,7 @@ # 46.全排列 -[力扣题目链接](https://leetcode-cn.com/problems/permutations/) +[力扣题目链接](https://leetcode.cn/problems/permutations/) 给定一个 没有重复 数字的序列,返回其所有可能的全排列。 diff --git "a/problems/0047.345円205円250円346円216円222円345円210円227円II.md" "b/problems/0047.345円205円250円346円216円222円345円210円227円II.md" index f635b8de40..f5f9e440b0 100644 --- "a/problems/0047.345円205円250円346円216円222円345円210円227円II.md" +++ "b/problems/0047.345円205円250円346円216円222円345円210円227円II.md" @@ -8,7 +8,7 @@ ## 47.全排列 II -[力扣题目链接](https://leetcode-cn.com/problems/permutations-ii/) +[力扣题目链接](https://leetcode.cn/problems/permutations-ii/) 给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。 diff --git "a/problems/0051.N347円232円207円345円220円216円.md" "b/problems/0051.N347円232円207円345円220円216円.md" index c03e48c2e4..798902aeb5 100644 --- "a/problems/0051.N347円232円207円345円220円216円.md" +++ "b/problems/0051.N347円232円207円345円220円216円.md" @@ -7,7 +7,7 @@ # 第51题. N皇后 -[力扣题目链接](https://leetcode-cn.com/problems/n-queens/) +[力扣题目链接](https://leetcode.cn/problems/n-queens/) n 皇后问题 研究的是如何将 n 个皇后放置在 n×ばつn 的棋盘上,并且使皇后彼此之间不能相互攻击。 diff --git "a/problems/0052.N347円232円207円345円220円216円II.md" "b/problems/0052.N347円232円207円345円220円216円II.md" index 67e439ca81..97d31a25af 100644 --- "a/problems/0052.N347円232円207円345円220円216円II.md" +++ "b/problems/0052.N347円232円207円345円220円216円II.md" @@ -8,7 +8,7 @@ # 52. N皇后II -题目链接:https://leetcode-cn.com/problems/n-queens-ii/ +题目链接:https://leetcode.cn/problems/n-queens-ii/ n 皇后问题研究的是如何将 n 个皇后放置在 n×ばつn 的棋盘上,并且使皇后彼此之间不能相互攻击。 @@ -44,7 +44,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×ばつn 的棋盘上,并 # 思路 -想看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别 +详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别 # C++代码 diff --git "a/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円.md" "b/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円.md" index 73cac244db..cbc91481bc 100644 --- "a/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円.md" +++ "b/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円.md" @@ -7,7 +7,7 @@ # 53. 最大子序和 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-subarray/) +[力扣题目链接](https://leetcode.cn/problems/maximum-subarray/) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 diff --git "a/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" "b/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" index 99aa7acfd5..345abc27d3 100644 --- "a/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" +++ "b/problems/0053.346円234円200円345円244円247円345円255円220円345円272円217円345円222円214円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" @@ -6,7 +6,7 @@ ## 53. 最大子序和 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-subarray/) +[力扣题目链接](https://leetcode.cn/problems/maximum-subarray/) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 diff --git "a/problems/0054.350円236円272円346円227円213円347円237円251円351円230円265円.md" "b/problems/0054.350円236円272円346円227円213円347円237円251円351円230円265円.md" index ccf6f47161..0d79fdf929 100644 --- "a/problems/0054.350円236円272円346円227円213円347円237円251円351円230円265円.md" +++ "b/problems/0054.350円236円272円346円227円213円347円237円251円351円230円265円.md" @@ -8,7 +8,7 @@ ## 54.螺旋矩阵 -[力扣题目链接](https://leetcode-cn.com/problems/spiral-matrix/) +[力扣题目链接](https://leetcode.cn/problems/spiral-matrix/) 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 @@ -128,8 +128,8 @@ public: ## 类似题目 -* [59.螺旋矩阵II](https://leetcode-cn.com/problems/spiral-matrix-ii/) -* [剑指Offer 29.顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) +* [59.螺旋矩阵II](https://leetcode.cn/problems/spiral-matrix-ii/) +* [剑指Offer 29.顺时针打印矩阵](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) ## 其他语言版本 Python: diff --git "a/problems/0055.350円267円263円350円267円203円346円270円270円346円210円217円.md" "b/problems/0055.350円267円263円350円267円203円346円270円270円346円210円217円.md" index 17a3b4f4d3..6fa8349514 100644 --- "a/problems/0055.350円267円263円350円267円203円346円270円270円346円210円217円.md" +++ "b/problems/0055.350円267円263円350円267円203円346円270円270円346円210円217円.md" @@ -7,7 +7,7 @@ # 55. 跳跃游戏 -[力扣题目链接](https://leetcode-cn.com/problems/jump-game/) +[力扣题目链接](https://leetcode.cn/problems/jump-game/) 给定一个非负整数数组,你最初位于数组的第一个位置。 diff --git "a/problems/0056.345円220円210円345円271円266円345円214円272円351円227円264円.md" "b/problems/0056.345円220円210円345円271円266円345円214円272円351円227円264円.md" index e444a22111..34d8dd8242 100644 --- "a/problems/0056.345円220円210円345円271円266円345円214円272円351円227円264円.md" +++ "b/problems/0056.345円220円210円345円271円266円345円214円272円351円227円264円.md" @@ -7,7 +7,7 @@ # 56. 合并区间 -[力扣题目链接](https://leetcode-cn.com/problems/merge-intervals/) +[力扣题目链接](https://leetcode.cn/problems/merge-intervals/) 给出一个区间的集合,请合并所有重叠的区间。 diff --git "a/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" "b/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" index 2222930212..bf0a279e25 100644 --- "a/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" +++ "b/problems/0059.350円236円272円346円227円213円347円237円251円351円230円265円II.md" @@ -8,7 +8,7 @@ ## 59.螺旋矩阵II -[力扣题目链接](https://leetcode-cn.com/problems/spiral-matrix-ii/) +[力扣题目链接](https://leetcode.cn/problems/spiral-matrix-ii/) 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 @@ -24,6 +24,8 @@ ## 思路 +为了利于录友们理解,我特意录制了视频,[拿下螺旋矩阵,《代码随想录》第五题!](https://www.bilibili.com/video/BV1SL4y1N7mV),结合本篇文章一起看,效果更佳。 + 这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。** 要如何画出这个螺旋排列的正方形矩阵呢? @@ -74,7 +76,7 @@ public: int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理 int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2) int count = 1; // 用来给矩阵中每一个空格赋值 - int offset = 1; // 每一圈循环,需要控制每一条边遍历的长度 + int offset = 1; // 需要控制每一条边遍历的长度,每次循环右边界收缩一位 int i,j; while (loop --) { i = startx; @@ -82,11 +84,11 @@ public: // 下面开始的四个for就是模拟转了一圈 // 模拟填充上行从左到右(左闭右开) - for (j = starty; j < starty + n - offset; j++) { + for (j = starty; j < n - offset; j++) { res[startx][j] = count++; } // 模拟填充右列从上到下(左闭右开) - for (i = startx; i < startx + n - offset; i++) { + for (i = startx; i < n - offset; i++) { res[i][j] = count++; } // 模拟填充下行从右到左(左闭右开) @@ -103,7 +105,7 @@ public: starty++; // offset 控制每一圈里每一条边遍历的长度 - offset += 2; + offset += 1; } // 如果n为奇数的话,需要单独给矩阵最中间的位置赋值 diff --git "a/problems/0062.344円270円215円345円220円214円350円267円257円345円276円204円.md" "b/problems/0062.344円270円215円345円220円214円350円267円257円345円276円204円.md" index f59b7be8bc..02bcc2aeea 100644 --- "a/problems/0062.344円270円215円345円220円214円350円267円257円345円276円204円.md" +++ "b/problems/0062.344円270円215円345円220円214円350円267円257円345円276円204円.md" @@ -6,7 +6,7 @@ # 62.不同路径 -[力扣题目链接](https://leetcode-cn.com/problems/unique-paths/) +[力扣题目链接](https://leetcode.cn/problems/unique-paths/) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 "Start" )。 diff --git "a/problems/0063.344円270円215円345円220円214円350円267円257円345円276円204円II.md" "b/problems/0063.344円270円215円345円220円214円350円267円257円345円276円204円II.md" index 59c60156b8..ca34055a60 100644 --- "a/problems/0063.344円270円215円345円220円214円350円267円257円345円276円204円II.md" +++ "b/problems/0063.344円270円215円345円220円214円350円267円257円345円276円204円II.md" @@ -6,7 +6,7 @@ # 63. 不同路径 II -[力扣题目链接](https://leetcode-cn.com/problems/unique-paths-ii/) +[力扣题目链接](https://leetcode.cn/problems/unique-paths-ii/) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" )。 diff --git "a/problems/0070.347円210円254円346円245円274円346円242円257円.md" "b/problems/0070.347円210円254円346円245円274円346円242円257円.md" index 34d414413a..c92c581c6e 100644 --- "a/problems/0070.347円210円254円346円245円274円346円242円257円.md" +++ "b/problems/0070.347円210円254円346円245円274円346円242円257円.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 70. 爬楼梯 -[力扣题目链接](https://leetcode-cn.com/problems/climbing-stairs/) +[力扣题目链接](https://leetcode.cn/problems/climbing-stairs/) 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 diff --git "a/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" "b/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" index 0f482bb7cb..ec019e5759 100644 --- "a/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" +++ "b/problems/0070.347円210円254円346円245円274円346円242円257円345円256円214円345円205円250円350円203円214円345円214円205円347円211円210円346円234円254円.md" @@ -11,7 +11,7 @@ ## 70. 爬楼梯 -[力扣题目链接](https://leetcode-cn.com/problems/climbing-stairs/) +[力扣题目链接](https://leetcode.cn/problems/climbing-stairs/) 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 diff --git "a/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" "b/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" index 530774eefc..641d31284d 100644 --- "a/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" +++ "b/problems/0072.347円274円226円350円276円221円350円267円235円347円246円273円.md" @@ -6,7 +6,7 @@ ## 72. 编辑距离 -[力扣题目链接](https://leetcode-cn.com/problems/edit-distance/) +[力扣题目链接](https://leetcode.cn/problems/edit-distance/) 给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。 diff --git "a/problems/0077.347円273円204円345円220円210円.md" "b/problems/0077.347円273円204円345円220円210円.md" index 9e0398aba1..8d22d0187f 100644 --- "a/problems/0077.347円273円204円345円220円210円.md" +++ "b/problems/0077.347円273円204円345円220円210円.md" @@ -9,7 +9,7 @@ # 第77题. 组合 -[力扣题目链接](https://leetcode-cn.com/problems/combinations/ ) +[力扣题目链接](https://leetcode.cn/problems/combinations/ ) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 diff --git "a/problems/0077.347円273円204円345円220円210円344円274円230円345円214円226円.md" "b/problems/0077.347円273円204円345円220円210円344円274円230円345円214円226円.md" index 94608ec18f..a6767047f4 100644 --- "a/problems/0077.347円273円204円345円220円210円344円274円230円345円214円226円.md" +++ "b/problems/0077.347円273円204円345円220円210円344円274円230円345円214円226円.md" @@ -14,7 +14,7 @@ 文中的回溯法是可以剪枝优化的,本篇我们继续来看一下题目77. 组合。 -链接:https://leetcode-cn.com/problems/combinations/ +链接:https://leetcode.cn/problems/combinations/ **看本篇之前,需要先看[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)**。 diff --git "a/problems/0078.345円255円220円351円233円206円.md" "b/problems/0078.345円255円220円351円233円206円.md" index 2b1c7643b1..3d850c0840 100644 --- "a/problems/0078.345円255円220円351円233円206円.md" +++ "b/problems/0078.345円255円220円351円233円206円.md" @@ -7,7 +7,7 @@ # 78.子集 -[力扣题目链接](https://leetcode-cn.com/problems/subsets/) +[力扣题目链接](https://leetcode.cn/problems/subsets/) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 diff --git "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" index 8f10a58251..e085e455cf 100644 --- "a/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" +++ "b/problems/0084.346円237円261円347円212円266円345円233円276円344円270円255円346円234円200円345円244円247円347円232円204円347円237円251円345円275円242円.md" @@ -7,7 +7,7 @@ # 84.柱状图中最大的矩形 -[力扣题目链接](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/) +[力扣题目链接](https://leetcode.cn/problems/largest-rectangle-in-histogram/) 给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 diff --git "a/problems/0090.345円255円220円351円233円206円II.md" "b/problems/0090.345円255円220円351円233円206円II.md" index dd64d1998e..909c569272 100644 --- "a/problems/0090.345円255円220円351円233円206円II.md" +++ "b/problems/0090.345円255円220円351円233円206円II.md" @@ -8,7 +8,7 @@ ## 90.子集II -[力扣题目链接](https://leetcode-cn.com/problems/subsets-ii/) +[力扣题目链接](https://leetcode.cn/problems/subsets-ii/) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 diff --git "a/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md" "b/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md" index d5eaa8abd0..41940487df 100644 --- "a/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md" +++ "b/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md" @@ -8,7 +8,7 @@ # 93.复原IP地址 -[力扣题目链接](https://leetcode-cn.com/problems/restore-ip-addresses/) +[力扣题目链接](https://leetcode.cn/problems/restore-ip-addresses/) 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 diff --git "a/problems/0096.344円270円215円345円220円214円347円232円204円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" "b/problems/0096.344円270円215円345円220円214円347円232円204円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" index 25561b506a..9d98c62dbc 100644 --- "a/problems/0096.344円270円215円345円220円214円347円232円204円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" +++ "b/problems/0096.344円270円215円345円220円214円347円232円204円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" @@ -6,7 +6,7 @@ # 96.不同的二叉搜索树 -[力扣题目链接](https://leetcode-cn.com/problems/unique-binary-search-trees/) +[力扣题目链接](https://leetcode.cn/problems/unique-binary-search-trees/) 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? diff --git "a/problems/0098.351円252円214円350円257円201円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" "b/problems/0098.351円252円214円350円257円201円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" index a8f3c32402..cba450e596 100644 --- "a/problems/0098.351円252円214円350円257円201円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" +++ "b/problems/0098.351円252円214円350円257円201円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" @@ -7,7 +7,7 @@ # 98.验证二叉搜索树 -[力扣题目链接](https://leetcode-cn.com/problems/validate-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/validate-binary-search-tree/) 给定一个二叉树,判断其是否是一个有效的二叉搜索树。 @@ -589,7 +589,50 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` +## Scala + +辅助数组解决: +```scala +object Solution { + import scala.collection.mutable + def isValidBST(root: TreeNode): Boolean = { + var arr = new mutable.ArrayBuffer[Int]() + // 递归中序遍历二叉树,将节点添加到arr + def traversal(node: TreeNode): Unit = { + if (node == null) return + traversal(node.left) + arr.append(node.value) + traversal(node.right) + } + traversal(root) + // 这个数组如果是升序就代表是二叉搜索树 + for (i <- 1 until arr.size) { + if (arr(i) <= arr(i - 1)) return false + } + true + } +} +``` +递归中解决: +```scala +object Solution { + def isValidBST(root: TreeNode): Boolean = { + var flag = true + var preValue:Long = Long.MinValue // 这里要使用Long类型 + + def traversal(node: TreeNode): Unit = { + if (node == null || flag == false) return + traversal(node.left) + if (node.value> preValue) preValue = node.value + else flag = false + traversal(node.right) + } + traversal(root) + flag + } +} +``` -----------------------
diff --git "a/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" "b/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" index 5e805d0111..5288779b7c 100644 --- "a/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" +++ "b/problems/0100.347円233円270円345円220円214円347円232円204円346円240円221円.md" @@ -8,7 +8,7 @@ # 100. 相同的树 -[力扣题目链接](https://leetcode-cn.com/problems/same-tree/) +[力扣题目链接](https://leetcode.cn/problems/same-tree/) 给定两个二叉树,编写一个函数来检验它们是否相同。 diff --git "a/problems/0101.345円257円271円347円247円260円344円272円214円345円217円211円346円240円221円.md" "b/problems/0101.345円257円271円347円247円260円344円272円214円345円217円211円346円240円221円.md" index c79fde0ec1..40249bb78d 100644 --- "a/problems/0101.345円257円271円347円247円260円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0101.345円257円271円347円247円260円344円272円214円345円217円211円346円240円221円.md" @@ -7,7 +7,7 @@ # 101. 对称二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/symmetric-tree/) +[力扣题目链接](https://leetcode.cn/problems/symmetric-tree/) 给定一个二叉树,检查它是否是镜像对称的。 diff --git "a/problems/0102.344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" "b/problems/0102.344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" index 3aa176991c..a2717d09ba 100644 --- "a/problems/0102.344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" +++ "b/problems/0102.344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" @@ -26,7 +26,7 @@ # 102.二叉树的层序遍历 -[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) +[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/) 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。 @@ -379,7 +379,7 @@ pub fn level_order(root: Option>>) -> Vec> { # 107.二叉树的层次遍历 II -[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/) +[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) @@ -660,7 +660,7 @@ pub fn level_order(root: Option>>) -> Vec> { # 199.二叉树的右视图 -[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/) +[力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/) 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 @@ -907,7 +907,7 @@ object Solution { # 637.二叉树的层平均值 -[力扣题目链接](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 @@ -1163,7 +1163,7 @@ object Solution { # 429.N叉树的层序遍历 -[力扣题目链接](https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/) +[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。 @@ -1434,7 +1434,7 @@ object Solution { # 515.在每个树行中找最大值 -[力扣题目链接](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/) +[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) 您需要在二叉树的每一行中找到最大的值。 @@ -1668,7 +1668,7 @@ object Solution { # 116.填充每个节点的下一个右侧节点指针 -[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/) +[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下: @@ -1956,7 +1956,7 @@ object Solution { ``` # 117.填充每个节点的下一个右侧节点指针II -[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/) +[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) 思路: @@ -2236,7 +2236,7 @@ object Solution { ``` # 104.二叉树的最大深度 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) 给定一个二叉树,找出其最大深度。 @@ -2477,7 +2477,7 @@ object Solution { # 111.二叉树的最小深度 -[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 diff --git "a/problems/0104.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円244円247円346円267円261円345円272円246円.md" "b/problems/0104.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円244円247円346円267円261円345円272円246円.md" index 4714685b2d..ed27f95d11 100644 --- "a/problems/0104.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円244円247円346円267円261円345円272円246円.md" +++ "b/problems/0104.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円244円247円346円267円261円345円272円246円.md" @@ -12,7 +12,7 @@ # 104.二叉树的最大深度 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) 给定一个二叉树,找出其最大深度。 @@ -223,7 +223,7 @@ impl Solution { # 559.n叉树的最大深度 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/) +[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) 给定一个 n 叉树,找到其最大深度。 diff --git "a/problems/0106.344円273円216円344円270円255円345円272円217円344円270円216円345円220円216円345円272円217円351円201円215円345円216円206円345円272円217円345円210円227円346円236円204円351円200円240円344円272円214円345円217円211円346円240円221円.md" "b/problems/0106.344円273円216円344円270円255円345円272円217円344円270円216円345円220円216円345円272円217円351円201円215円345円216円206円345円272円217円345円210円227円346円236円204円351円200円240円344円272円214円345円217円211円346円240円221円.md" index 188ad3cb44..6d87c756bd 100644 --- "a/problems/0106.344円273円216円344円270円255円345円272円217円344円270円216円345円220円216円345円272円217円351円201円215円345円216円206円345円272円217円345円210円227円346円236円204円351円200円240円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0106.344円273円216円344円270円255円345円272円217円344円270円216円345円220円216円345円272円217円351円201円215円345円216円206円345円272円217円345円210円227円346円236円204円351円200円240円344円272円214円345円217円211円346円240円221円.md" @@ -12,7 +12,7 @@ # 106.从中序与后序遍历序列构造二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) +[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) 根据一棵树的中序遍历与后序遍历构造二叉树。 @@ -394,7 +394,7 @@ public: # 105.从前序与中序遍历序列构造二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) +[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) 根据一棵树的前序遍历与中序遍历构造二叉树。 @@ -1091,7 +1091,53 @@ class Solution_0106 { } ``` +## Scala +106 从中序与后序遍历序列构造二叉树 + +```scala +object Solution { + def buildTree(inorder: Array[Int], postorder: Array[Int]): TreeNode = { + // 1、如果长度为0,则直接返回null + var len = inorder.size + if (len == 0) return null + // 2、后序数组的最后一个元素是当前根元素 + var rootValue = postorder(len - 1) + var root: TreeNode = new TreeNode(rootValue, null, null) + if (len == 1) return root // 如果数组只有一个节点,就直接返回 + // 3、在中序数组中找到切割点的索引 + var delimiterIndex: Int = inorder.indexOf(rootValue) + // 4、切分数组往下迭代 + root.left = buildTree(inorder.slice(0, delimiterIndex), postorder.slice(0, delimiterIndex)) + root.right = buildTree(inorder.slice(delimiterIndex + 1, len), postorder.slice(delimiterIndex, len - 1)) + root // 返回root,return关键字可以省略 + } +} +``` + +105 从前序与中序遍历序列构造二叉树 + +```scala +object Solution { + def buildTree(preorder: Array[Int], inorder: Array[Int]): TreeNode = { + // 1、如果长度为0,直接返回空 + var len = inorder.size + if (len == 0) return null + // 2、前序数组的第一个元素是当前子树根节点 + var rootValue = preorder(0) + var root = new TreeNode(rootValue, null, null) + if (len == 1) return root // 如果数组元素只有一个,那么返回根节点 + // 3、在中序数组中,找到切割点 + var delimiterIndex = inorder.indexOf(rootValue) + + // 4、切分数组往下迭代 + root.left = buildTree(preorder.slice(1, delimiterIndex + 1), inorder.slice(0, delimiterIndex)) + root.right = buildTree(preorder.slice(delimiterIndex + 1, preorder.size), inorder.slice(delimiterIndex + 1, len)) + + root + } +} +``` -----------------------
diff --git "a/problems/0108.345円260円206円346円234円211円345円272円217円346円225円260円347円273円204円350円275円254円346円215円242円344円270円272円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" "b/problems/0108.345円260円206円346円234円211円345円272円217円346円225円260円347円273円204円350円275円254円346円215円242円344円270円272円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" index 6ee3947bec..c9c1a69335 100644 --- "a/problems/0108.345円260円206円346円234円211円345円272円217円346円225円260円347円273円204円350円275円254円346円215円242円344円270円272円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" +++ "b/problems/0108.345円260円206円346円234円211円345円272円217円346円225円260円347円273円204円350円275円254円346円215円242円344円270円272円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" @@ -9,7 +9,7 @@ # 108.将有序数组转换为二叉搜索树 -[力扣题目链接](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/) 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。 diff --git "a/problems/0110.345円271円263円350円241円241円344円272円214円345円217円211円346円240円221円.md" "b/problems/0110.345円271円263円350円241円241円344円272円214円345円217円211円346円240円221円.md" index b759836551..3aa815ab07 100644 --- "a/problems/0110.345円271円263円350円241円241円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0110.345円271円263円350円241円241円344円272円214円345円217円211円346円240円221円.md" @@ -9,7 +9,7 @@ # 110.平衡二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/balanced-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/balanced-binary-tree/) 给定一个二叉树,判断它是否是高度平衡的二叉树。 diff --git "a/problems/0111.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円260円217円346円267円261円345円272円246円.md" "b/problems/0111.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円260円217円346円267円261円345円272円246円.md" index da24480add..0137bd15b1 100644 --- "a/problems/0111.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円260円217円346円267円261円345円272円246円.md" +++ "b/problems/0111.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円345円260円217円346円267円261円345円272円246円.md" @@ -9,7 +9,7 @@ # 111.二叉树的最小深度 -[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) 给定一个二叉树,找出其最小深度。 diff --git "a/problems/0112.350円267円257円345円276円204円346円200円273円345円222円214円.md" "b/problems/0112.350円267円257円345円276円204円346円200円273円345円222円214円.md" index d3eec16b9e..d4cb51908d 100644 --- "a/problems/0112.350円267円257円345円276円204円346円200円273円345円222円214円.md" +++ "b/problems/0112.350円267円257円345円276円204円346円200円273円345円222円214円.md" @@ -16,7 +16,7 @@ # 112. 路径总和 -[力扣题目链接](https://leetcode-cn.com/problems/path-sum/) +[力扣题目链接](https://leetcode.cn/problems/path-sum/) 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。 @@ -216,7 +216,7 @@ public: # 113. 路径总和ii -[力扣题目链接](https://leetcode-cn.com/problems/path-sum-ii/) +[力扣题目链接](https://leetcode.cn/problems/path-sum-ii/) 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 @@ -300,7 +300,7 @@ public: ## java -lc112 +### 0112.路径总和 ```java class solution { public boolean haspathsum(treenode root, int targetsum) { @@ -373,7 +373,7 @@ class solution { ``` -0113.路径总和-ii +### 0113.路径总和-ii ```java class solution { @@ -436,7 +436,7 @@ class Solution { ## python -0112.路径总和 +### 0112.路径总和 **递归** ```python @@ -488,7 +488,7 @@ class solution: return false ``` -0113.路径总和-ii +### 0113.路径总和-ii **递归** ```python @@ -545,7 +545,7 @@ class Solution: ## go -112. 路径总和 +### 112. 路径总和 ```go //递归法 @@ -570,7 +570,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -113. 路径总和 II +### 113. 路径总和 II ```go /** @@ -612,7 +612,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { ## javascript -0112.路径总和 +### 0112.路径总和 **递归** ```javascript @@ -673,7 +673,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -0113.路径总和-ii +### 0113.路径总和-ii **递归** ```javascript @@ -768,7 +768,7 @@ let pathSum = function(root, targetSum) { ## TypeScript -> 0112.路径总和 +### 0112.路径总和 **递归法:** @@ -850,7 +850,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -> 0112.路径总和 ii +### 0112.路径总和 ii **递归法:** @@ -888,7 +888,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { ## Swift -0112.路径总和 +### 0112.路径总和 **递归** @@ -955,7 +955,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -0113.路径总和 II +### 0113.路径总和 II **递归** @@ -1126,7 +1126,90 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur } ``` +## Scala + +### 0112.路径总和 + +**递归:** +```scala +object Solution { + def hasPathSum(root: TreeNode, targetSum: Int): Boolean = { + if(root == null) return false + var res = false + + def traversal(curNode: TreeNode, sum: Int): Unit = { + if (res) return // 如果直接标记为true了,就没有往下递归的必要了 + if (curNode.left == null && curNode.right == null && sum == targetSum) { + res = true + return + } + // 往下递归 + if (curNode.left != null) traversal(curNode.left, sum + curNode.left.value) + if (curNode.right != null) traversal(curNode.right, sum + curNode.right.value) + } + traversal(root, root.value) + res // return关键字可以省略 + } +} +``` + +**迭代:** +```scala +object Solution { + import scala.collection.mutable + def hasPathSum(root: TreeNode, targetSum: Int): Boolean = { + if (root == null) return false + val stack = mutable.Stack[(TreeNode, Int)]() + stack.push((root, root.value)) // 将根节点元素放入stack + while (!stack.isEmpty) { + val curNode = stack.pop() // 取出栈顶元素 + // 如果遇到叶子节点,看当前的值是否等于targetSum,等于则返回true + if (curNode._1.left == null && curNode._1.right == null && curNode._2 == targetSum) { + return true + } + if (curNode._1.right != null) stack.push((curNode._1.right, curNode._2 + curNode._1.right.value)) + if (curNode._1.left != null) stack.push((curNode._1.left, curNode._2 + curNode._1.left.value)) + } + false //如果没有返回true,即可返回false,return关键字可以省略 + } +} +``` + +### 0113.路径总和 II + +**递归:** +```scala +object Solution { + import scala.collection.mutable.ListBuffer + def pathSum(root: TreeNode, targetSum: Int): List[List[Int]] = { + val res = ListBuffer[List[Int]]() + if (root == null) return res.toList + val path = ListBuffer[Int](); + + def traversal(cur: TreeNode, count: Int): Unit = { + if (cur.left == null && cur.right == null && count == 0) { + res.append(path.toList) + return + } + if (cur.left != null) { + path.append(cur.left.value) + traversal(cur.left, count - cur.left.value) + path.remove(path.size - 1) + } + if (cur.right != null) { + path.append(cur.right.value) + traversal(cur.right, count - cur.right.value) + path.remove(path.size - 1) + } + } + + path.append(root.value) + traversal(root, targetSum - root.value) + res.toList + } +} +``` -----------------------
diff --git "a/problems/0115.344円270円215円345円220円214円347円232円204円345円255円220円345円272円217円345円210円227円.md" "b/problems/0115.344円270円215円345円220円214円347円232円204円345円255円220円345円272円217円345円210円227円.md" index ca66e20d2a..9ae0413974 100644 --- "a/problems/0115.344円270円215円345円220円214円347円232円204円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0115.344円270円215円345円220円214円347円232円204円345円255円220円345円272円217円345円210円227円.md" @@ -6,7 +6,7 @@ ## 115.不同的子序列 -[力扣题目链接](https://leetcode-cn.com/problems/distinct-subsequences/) +[力扣题目链接](https://leetcode.cn/problems/distinct-subsequences/) 给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。 diff --git "a/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" "b/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" index 2c443de52a..ed8ce59217 100644 --- "a/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" +++ "b/problems/0116.345円241円253円345円205円205円346円257円217円344円270円252円350円212円202円347円202円271円347円232円204円344円270円213円344円270円200円344円270円252円345円217円263円344円276円247円350円212円202円347円202円271円346円214円207円351円222円210円.md" @@ -7,7 +7,7 @@ # 116. 填充每个节点的下一个右侧节点指针 -[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/) +[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) 给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下: diff --git "a/problems/0121.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円.md" "b/problems/0121.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円.md" index a2498bb6b6..a577f1dda2 100644 --- "a/problems/0121.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円.md" +++ "b/problems/0121.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円.md" @@ -6,7 +6,7 @@ ## 121. 买卖股票的最佳时机 -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/) 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 diff --git "a/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II.md" "b/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II.md" index 1e7b77d80a..b9fa8386ea 100644 --- "a/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II.md" +++ "b/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II.md" @@ -7,7 +7,7 @@ # 122.买卖股票的最佳时机II -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/) 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 diff --git "a/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" "b/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" index 12b21fde30..fa9f8842fd 100644 --- "a/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" +++ "b/problems/0122.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円II357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" @@ -6,7 +6,7 @@ ## 122.买卖股票的最佳时机II -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/) 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 diff --git "a/problems/0123.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円III.md" "b/problems/0123.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円III.md" index 67c9949719..c15aaee806 100644 --- "a/problems/0123.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円III.md" +++ "b/problems/0123.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円III.md" @@ -6,7 +6,7 @@ ## 123.买卖股票的最佳时机III -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/) 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 diff --git "a/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" "b/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" index 584bcb2a15..f1c6f1828a 100644 --- "a/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" +++ "b/problems/0127.345円215円225円350円257円215円346円216円245円351円276円231円.md" @@ -7,7 +7,7 @@ # 127. 单词接龙 -[力扣题目链接](https://leetcode-cn.com/problems/word-ladder/) +[力扣题目链接](https://leetcode.cn/problems/word-ladder/) 字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列: * 序列中第一个单词是 beginWord 。 diff --git "a/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" "b/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" index b271ca7de5..ea3845b741 100644 --- "a/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" +++ "b/problems/0129.346円261円202円346円240円271円345円210円260円345円217円266円345円255円220円350円212円202円347円202円271円346円225円260円345円255円227円344円271円213円345円222円214円.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 129. 求根节点到叶节点数字之和 -[力扣题目链接](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/) +[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) # 思路 diff --git "a/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md" "b/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md" index 6a370fb470..a371864d99 100644 --- "a/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md" +++ "b/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md" @@ -9,7 +9,7 @@ # 131.分割回文串 -[力扣题目链接](https://leetcode-cn.com/problems/palindrome-partitioning/) +[力扣题目链接](https://leetcode.cn/problems/palindrome-partitioning/) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 diff --git "a/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" "b/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" index 87d3e4b434..92f9b60238 100644 --- "a/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" +++ "b/problems/0132.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円II.md" @@ -8,7 +8,7 @@ # 132. 分割回文串 II -[力扣题目链接](https://leetcode-cn.com/problems/palindrome-partitioning-ii/) +[力扣题目链接](https://leetcode.cn/problems/palindrome-partitioning-ii/) 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文。 diff --git "a/problems/0134.345円212円240円346円262円271円347円253円231円.md" "b/problems/0134.345円212円240円346円262円271円347円253円231円.md" index a88f677d34..e6dec44be4 100644 --- "a/problems/0134.345円212円240円346円262円271円347円253円231円.md" +++ "b/problems/0134.345円212円240円346円262円271円347円253円231円.md" @@ -7,7 +7,7 @@ # 134. 加油站 -[力扣题目链接](https://leetcode-cn.com/problems/gas-station/) +[力扣题目链接](https://leetcode.cn/problems/gas-station/) 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。 diff --git "a/problems/0135.345円210円206円345円217円221円347円263円226円346円236円234円.md" "b/problems/0135.345円210円206円345円217円221円347円263円226円346円236円234円.md" index 3456a04c29..55c2efc77c 100644 --- "a/problems/0135.345円210円206円345円217円221円347円263円226円346円236円234円.md" +++ "b/problems/0135.345円210円206円345円217円221円347円263円226円346円236円234円.md" @@ -7,7 +7,7 @@ # 135. 分发糖果 -[力扣题目链接](https://leetcode-cn.com/problems/candy/) +[力扣题目链接](https://leetcode.cn/problems/candy/) 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。 diff --git "a/problems/0139.345円215円225円350円257円215円346円213円206円345円210円206円.md" "b/problems/0139.345円215円225円350円257円215円346円213円206円345円210円206円.md" index 5b4e92b9aa..7ff13f72a9 100644 --- "a/problems/0139.345円215円225円350円257円215円346円213円206円345円210円206円.md" +++ "b/problems/0139.345円215円225円350円257円215円346円213円206円345円210円206円.md" @@ -8,7 +8,7 @@ ## 139.单词拆分 -[力扣题目链接](https://leetcode-cn.com/problems/word-break/) +[力扣题目链接](https://leetcode.cn/problems/word-break/) 给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。 diff --git "a/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md" "b/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md" index f8e62d4572..6b7c7e661f 100644 --- "a/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md" +++ "b/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md" @@ -11,7 +11,7 @@ ## 142.环形链表II -[力扣题目链接](https://leetcode-cn.com/problems/linked-list-cycle-ii/) +[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/) 题意: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 diff --git "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" index 5452c30415..05916135be 100644 --- "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" +++ "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" @@ -11,7 +11,7 @@ # 150. 逆波兰表达式求值 -[力扣题目链接](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/) +[力扣题目链接](https://leetcode.cn/problems/evaluate-reverse-polish-notation/) 根据 逆波兰表示法,求表达式的值。 diff --git "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" index 0e25fc4d8c..38372f91e4 100644 --- "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" +++ "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" @@ -10,7 +10,7 @@ # 151.翻转字符串里的单词 -[力扣题目链接](https://leetcode-cn.com/problems/reverse-words-in-a-string/) +[力扣题目链接](https://leetcode.cn/problems/reverse-words-in-a-string/) 给定一个字符串,逐个翻转字符串中的每个单词。 @@ -234,7 +234,7 @@ public: } void removeExtraSpaces(string& s) {//去除所有空格并在相邻单词之间添加空格, 快慢指针。 - int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode-cn.com/problems/remove-element/ + int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode.cn/problems/remove-element/ for (int i = 0; i < s.size(); ++i) { // if (s[i] != ' ') { //遇到非空格就处理,即删除所有空格。 if (slow != 0) s[slow++] = ' '; //手动控制空格,给单词之间添加空格。slow != 0说明不是第一个单词,需要在单词前添加空格。 @@ -817,6 +817,53 @@ object Solution { ``` +PHP: +```php +function reverseWords($s) { + $this->removeExtraSpaces($s); + $this->reverseString($s, 0, strlen($s)-1); + // 将每个单词反转 + $start = 0; + for ($i = 0; $i <= strlen($s); $i++) { + // 到达空格或者串尾,说明一个单词结束。进行翻转。 + if ($i == strlen($s) || $s[$i] == ' ') { + // 翻转,注意是左闭右闭 []的翻转。 + $this->reverseString($s, $start, $i-1); + // +1: 单词与单词直接有个空格 + $start = $i + 1; + } + } + return $s; +} + +// 移除多余空格 +function removeExtraSpaces(&$s){ + $slow = 0; + for ($i = 0; $i < strlen($s); $i++) { + if ($s[$i] != ' ') { + if ($slow != 0){ + $s[$slow++] = ' '; + } + while ($i < strlen($s) && $s[$i] != ' ') { + $s[$slow++] = $s[$i++]; + } + } + } + // 移动覆盖处理,丢弃多余的脏数据。 + $s = substr($s,0,$slow); + return ; +} + +// 翻转字符串 +function reverseString(&$s, $start, $end) { + for ($i = $start, $j = $end; $i < $j; $i++, $j--) { + $tmp = $s[$i]; + $s[$i] = $s[$j]; + $s[$j] = $tmp; + } + return ; +} +``` ----------------------- diff --git "a/problems/0188.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円IV.md" "b/problems/0188.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円IV.md" index 27eb38c365..7ff198527b 100644 --- "a/problems/0188.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円IV.md" +++ "b/problems/0188.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円IV.md" @@ -6,7 +6,7 @@ ## 188.买卖股票的最佳时机IV -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/) 给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。 diff --git "a/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" "b/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" index 3ffed87745..23092f9cb7 100644 --- "a/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" +++ "b/problems/0189.346円227円213円350円275円254円346円225円260円347円273円204円.md" @@ -7,6 +7,8 @@ # 189. 旋转数组 +[力扣题目链接](https://leetcode.cn/problems/rotate-array/) + 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 进阶: @@ -160,6 +162,27 @@ var rotate = function (nums, k) { }; ``` +## TypeScript + +```typescript +function rotate(nums: number[], k: number): void { + const length: number = nums.length; + k %= length; + reverseByRange(nums, 0, length - 1); + reverseByRange(nums, 0, k - 1); + reverseByRange(nums, k, length - 1); +}; +function reverseByRange(nums: number[], left: number, right: number): void { + while (left < right) { + const temp = nums[left]; + nums[left] = nums[right]; + nums[right] = temp; + left++; + right--; + } +} +``` + ----------------------- diff --git "a/problems/0198.346円211円223円345円256円266円345円212円253円350円210円215円.md" "b/problems/0198.346円211円223円345円256円266円345円212円253円350円210円215円.md" index a828b9a9d6..ad660f2790 100644 --- "a/problems/0198.346円211円223円345円256円266円345円212円253円350円210円215円.md" +++ "b/problems/0198.346円211円223円345円256円266円345円212円253円350円210円215円.md" @@ -6,7 +6,7 @@ ## 198.打家劫舍 -[力扣题目链接](https://leetcode-cn.com/problems/house-robber/) +[力扣题目链接](https://leetcode.cn/problems/house-robber/) 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 diff --git "a/problems/0202.345円277円253円344円271円220円346円225円260円.md" "b/problems/0202.345円277円253円344円271円220円346円225円260円.md" index 7738b2f695..0bea0c7233 100644 --- "a/problems/0202.345円277円253円344円271円220円346円225円260円.md" +++ "b/problems/0202.345円277円253円344円271円220円346円225円260円.md" @@ -10,7 +10,7 @@ # 第202题. 快乐数 -[力扣题目链接](https://leetcode-cn.com/problems/happy-number/) +[力扣题目链接](https://leetcode.cn/problems/happy-number/) 编写一个算法来判断一个数 n 是不是快乐数。 diff --git "a/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md" "b/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md" index fe78ddab7c..b79d29a50d 100644 --- "a/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md" +++ "b/problems/0203.347円247円273円351円231円244円351円223円276円350円241円250円345円205円203円347円264円240円.md" @@ -9,7 +9,7 @@ # 203.移除链表元素 -[力扣题目链接](https://leetcode-cn.com/problems/remove-linked-list-elements/) +[力扣题目链接](https://leetcode.cn/problems/remove-linked-list-elements/) 题意:删除链表中等于给定值 val 的所有节点。 @@ -28,6 +28,8 @@ # 思路 +为了方便大家理解,我特意录制了视频:[手把手带你学会操作链表,移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。 + 这里以链表 1 4 2 4 来举例,移除元素4。 ![203_链表删除元素1](https://img-blog.csdnimg.cn/20210316095351161.png) diff --git "a/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" "b/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" index d4b71c59c4..284e7fd944 100644 --- "a/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0205.345円220円214円346円236円204円345円255円227円347円254円246円344円270円262円.md" @@ -7,7 +7,7 @@ # 205. 同构字符串 -[力扣题目链接](https://leetcode-cn.com/problems/isomorphic-strings/) +[力扣题目链接](https://leetcode.cn/problems/isomorphic-strings/) 给定两个字符串 s 和 t,判断它们是否是同构的。 diff --git "a/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" "b/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" index 25b16907e4..24ec7b943a 100644 --- "a/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" +++ "b/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" @@ -9,7 +9,7 @@ # 206.反转链表 -[力扣题目链接](https://leetcode-cn.com/problems/reverse-linked-list/) +[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/) 题意:反转一个单链表。 @@ -496,8 +496,26 @@ struct ListNode* reverseList(struct ListNode* head){ return reverse(NULL, head); } ``` -Scala: + + +PHP: +```php +// 双指针法: +function reverseList($head) { + $cur = $head; + $pre = NULL; + while($cur){ + $temp = $cur->next; + $cur->next = $pre; + $pre = $cur; + $cur = $temp; + } + return $pre; + } +``` + +Scala: 双指针法: ```scala object Solution { @@ -529,6 +547,7 @@ object Solution { cur.next = pre reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 } + } ``` ----------------------- diff --git "a/problems/0209.351円225円277円345円272円246円346円234円200円345円260円217円347円232円204円345円255円220円346円225円260円347円273円204円.md" "b/problems/0209.351円225円277円345円272円246円346円234円200円345円260円217円347円232円204円345円255円220円346円225円260円347円273円204円.md" index 69e0da4f1d..e2eb378e7a 100644 --- "a/problems/0209.351円225円277円345円272円246円346円234円200円345円260円217円347円232円204円345円255円220円346円225円260円347円273円204円.md" +++ "b/problems/0209.351円225円277円345円272円246円346円234円200円345円260円217円347円232円204円345円255円220円346円225円260円347円273円204円.md" @@ -7,7 +7,7 @@ # 209.长度最小的子数组 -[力扣题目链接](https://leetcode-cn.com/problems/minimum-size-subarray-sum/) +[力扣题目链接](https://leetcode.cn/problems/minimum-size-subarray-sum/) 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。 @@ -133,8 +133,8 @@ public: ## 相关题目推荐 -* [904.水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/) -* [76.最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring/) +* [904.水果成篮](https://leetcode.cn/problems/fruit-into-baskets/) +* [76.最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/) diff --git "a/problems/0213.346円211円223円345円256円266円345円212円253円350円210円215円II.md" "b/problems/0213.346円211円223円345円256円266円345円212円253円350円210円215円II.md" index 9e698d01c0..d6b53a24cf 100644 --- "a/problems/0213.346円211円223円345円256円266円345円212円253円350円210円215円II.md" +++ "b/problems/0213.346円211円223円345円256円266円345円212円253円350円210円215円II.md" @@ -6,7 +6,7 @@ ## 213.打家劫舍II -[力扣题目链接](https://leetcode-cn.com/problems/house-robber-ii/) +[力扣题目链接](https://leetcode.cn/problems/house-robber-ii/) 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。 diff --git "a/problems/0216.347円273円204円345円220円210円346円200円273円345円222円214円III.md" "b/problems/0216.347円273円204円345円220円210円346円200円273円345円222円214円III.md" index 66ca7ff7e9..0ace0fd5af 100644 --- "a/problems/0216.347円273円204円345円220円210円346円200円273円345円222円214円III.md" +++ "b/problems/0216.347円273円204円345円220円210円346円200円273円345円222円214円III.md" @@ -11,7 +11,7 @@ # 216.组合总和III -[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-iii/) +[力扣题目链接](https://leetcode.cn/problems/combination-sum-iii/) 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。 diff --git "a/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md" "b/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md" index 746d45cc4a..e2825cfb30 100644 --- "a/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md" +++ "b/problems/0222.345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円347円232円204円350円212円202円347円202円271円344円270円252円346円225円260円.md" @@ -7,7 +7,7 @@ # 222.完全二叉树的节点个数 -[力扣题目链接](https://leetcode-cn.com/problems/count-complete-tree-nodes/) +[力扣题目链接](https://leetcode.cn/problems/count-complete-tree-nodes/) 给出一个完全二叉树,求出该树的节点个数。 diff --git "a/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md" "b/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md" index 3c134870ae..40415d8dde 100644 --- "a/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md" +++ "b/problems/0225.347円224円250円351円230円237円345円210円227円345円256円236円347円216円260円346円240円210円.md" @@ -10,7 +10,7 @@ # 225. 用队列实现栈 -[力扣题目链接](https://leetcode-cn.com/problems/implement-stack-using-queues/) +[力扣题目链接](https://leetcode.cn/problems/implement-stack-using-queues/) 使用队列实现栈的下列操作: @@ -816,7 +816,6 @@ class MyStack { } ``` Scala: - 使用两个队列模拟栈: ```scala import scala.collection.mutable @@ -897,6 +896,86 @@ class MyStack() { def empty(): Boolean = { queue.isEmpty } + } +``` + + +PHP +> 双对列 +```php +// SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5>= 5.3.0, PHP 7, PHP 8) +// https://www.php.net/manual/zh/class.splqueue.php +class MyStack { + public $queueMain; // 保存数据 + public $queueTmp; // 辅助作用 + + function __construct() { + $this->queueMain=new SplQueue(); + $this->queueTmp=new SplQueue(); + } + + // queueMain: 1,2,3 <= add + function push($x) { + $this->queueMain->enqueue($x); + } + + function pop() { + $qmSize = $this->queueMain->Count(); + $qmSize --; + // queueMain: 3,2,1 => pop =>2,1 => add => 2,1 :queueTmp + while($qmSize --){ + $this->queueTmp->enqueue($this->queueMain->dequeue()); + } + // queueMain: 3 + $val = $this->queueMain->dequeue(); + // queueMain <= queueTmp + $this->queueMain = $this->queueTmp; + // 清空queueTmp,下次使用 + $this->queueTmp = new SplQueue(); + return $val; + } + + function top() { + // 底层是双链表实现:从双链表的末尾查看节点 + return $this->queueMain->top(); + } + + function empty() { + return $this->queueMain->isEmpty(); + } +} +``` +> 单对列 +```php +class MyStack { + public $queue; + + function __construct() { + $this->queue=new SplQueue(); + } + + function push($x) { + $this->queue->enqueue($x); + } + + function pop() { + $qmSize = $this->queue->Count(); + $qmSize --; + //queue: 3,2,1 => pop =>2,1 => add => 2,1,3 :queue + while($qmSize --){ + $this->queue->enqueue($this->queue->dequeue()); + } + $val = $this->queue->dequeue(); + return $val; + } + + function top() { + return $this->queue->top(); + } + + function empty() { + return $this->queue->isEmpty(); + } } ``` ----------------------- diff --git "a/problems/0226.347円277円273円350円275円254円344円272円214円345円217円211円346円240円221円.md" "b/problems/0226.347円277円273円350円275円254円344円272円214円345円217円211円346円240円221円.md" index 9b5cfbaef7..8e35fc9d33 100644 --- "a/problems/0226.347円277円273円350円275円254円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0226.347円277円273円350円275円254円344円272円214円345円217円211円346円240円221円.md" @@ -7,7 +7,7 @@ # 226.翻转二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/invert-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/invert-binary-tree/) 翻转一棵二叉树。 diff --git "a/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md" "b/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md" index d9ba8e2632..4662e2f28c 100644 --- "a/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md" +++ "b/problems/0232.347円224円250円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.md" @@ -9,7 +9,7 @@ # 232.用栈实现队列 -[力扣题目链接](https://leetcode-cn.com/problems/implement-queue-using-stacks/) +[力扣题目链接](https://leetcode.cn/problems/implement-queue-using-stacks/) 使用栈实现队列的下列操作: @@ -495,6 +495,53 @@ void myQueueFree(MyQueue* obj) { obj->stackOutTop = 0; } ``` + + +PHP: +```php +// SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5>= 5.3.0, PHP 7, PHP 8] +// https://www.php.net/manual/zh/class.splstack.php +class MyQueue { + // 双栈模拟队列:In栈存储数据;Out栈辅助处理 + private $stackIn; + private $stackOut; + + function __construct() { + $this->stackIn = new SplStack(); + $this->stackOut = new SplStack(); + } + + // In: 1 2 3 <= push + function push($x) { + $this->stackIn->push($x); + } + + function pop() { + $this->peek(); + return $this->stackOut->pop(); + } + + function peek() { + if($this->stackOut->isEmpty()){ + $this->shift(); + } + return $this->stackOut->top(); + } + + function empty() { + return $this->stackOut->isEmpty() && $this->stackIn->isEmpty(); + } + + // 如果Out栈为空,把In栈数据压入Out栈 + // In: 1 2 3 => pop push => 1 2 3 :Out + private function shift(){ + while(!$this->stackIn->isEmpty()){ + $this->stackOut->push($this->stackIn->pop()); + } + } + } +``` + Scala: ```scala class MyQueue() { @@ -533,6 +580,7 @@ class MyQueue() { def empty(): Boolean = { stackIn.isEmpty && stackOut.isEmpty } + } ``` ----------------------- diff --git "a/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" "b/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" index db910d4ee0..f591fcef2b 100644 --- "a/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" +++ "b/problems/0234.345円233円236円346円226円207円351円223円276円350円241円250円.md" @@ -7,7 +7,7 @@ # 234.回文链表 -[力扣题目链接](https://leetcode-cn.com/problems/palindrome-linked-list/) +[力扣题目链接](https://leetcode.cn/problems/palindrome-linked-list/) 请判断一个链表是否为回文链表。 diff --git "a/problems/0235.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" "b/problems/0235.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" index f7f1427aad..9ff7e2938f 100644 --- "a/problems/0235.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" +++ "b/problems/0235.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" @@ -7,7 +7,7 @@ # 235. 二叉搜索树的最近公共祖先 -[力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/) 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 diff --git "a/problems/0236.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" "b/problems/0236.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" index 69a6d0d6a7..23695b11a9 100644 --- "a/problems/0236.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" +++ "b/problems/0236.344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円350円277円221円345円205円254円345円205円261円347円245円226円345円205円210円.md" @@ -9,7 +9,7 @@ # 236. 二叉树的最近公共祖先 -[力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) +[力扣题目链接](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/) 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 diff --git "a/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" "b/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" index eb32fdd279..23e79c8096 100644 --- "a/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" +++ "b/problems/0239.346円273円221円345円212円250円347円252円227円345円217円243円346円234円200円345円244円247円345円200円274円.md" @@ -10,7 +10,7 @@ # 239. 滑动窗口最大值 -[力扣题目链接](https://leetcode-cn.com/problems/sliding-window-maximum/) +[力扣题目链接](https://leetcode.cn/problems/sliding-window-maximum/) 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 diff --git "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" index 8fd9c6048d..8f4b5ae2b7 100644 --- "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" +++ "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" @@ -9,7 +9,7 @@ ## 242.有效的字母异位词 -[力扣题目链接](https://leetcode-cn.com/problems/valid-anagram/) +[力扣题目链接](https://leetcode.cn/problems/valid-anagram/) 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 diff --git "a/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md" "b/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md" index 70a3c66f5d..d84cc6e106 100644 --- "a/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md" +++ "b/problems/0257.344円272円214円345円217円211円346円240円221円347円232円204円346円211円200円346円234円211円350円267円257円345円276円204円.md" @@ -9,7 +9,7 @@ # 257. 二叉树的所有路径 -[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-paths/) +[力扣题目链接](https://leetcode.cn/problems/binary-tree-paths/) 给定一个二叉树,返回所有从根节点到叶子节点的路径。 diff --git "a/problems/0279.345円256円214円345円205円250円345円271円263円346円226円271円346円225円260円.md" "b/problems/0279.345円256円214円345円205円250円345円271円263円346円226円271円346円225円260円.md" index 5b15639c06..e8ec98c6c7 100644 --- "a/problems/0279.345円256円214円345円205円250円345円271円263円346円226円271円346円225円260円.md" +++ "b/problems/0279.345円256円214円345円205円250円345円271円263円346円226円271円346円225円260円.md" @@ -7,7 +7,7 @@ ## 279.完全平方数 -[力扣题目链接](https://leetcode-cn.com/problems/perfect-squares/) +[力扣题目链接](https://leetcode.cn/problems/perfect-squares/) 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。 diff --git "a/problems/0283.347円247円273円345円212円250円351円233円266円.md" "b/problems/0283.347円247円273円345円212円250円351円233円266円.md" index ed59d2c4aa..fe8e41c1a2 100644 --- "a/problems/0283.347円247円273円345円212円250円351円233円266円.md" +++ "b/problems/0283.347円247円273円345円212円250円351円233円266円.md" @@ -8,7 +8,7 @@ # 283. 移动零 -[力扣题目链接](https://leetcode-cn.com/problems/move-zeroes/) +[力扣题目链接](https://leetcode.cn/problems/move-zeroes/) 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 @@ -133,6 +133,27 @@ var moveZeroes = function(nums) { }; ``` +TypeScript: + +```typescript +function moveZeroes(nums: number[]): void { + const length: number = nums.length; + let slowIndex: number = 0, + fastIndex: number = 0; + while (fastIndex < length) { + if (nums[fastIndex] !== 0) { + nums[slowIndex++] = nums[fastIndex]; + }; + fastIndex++; + } + while (slowIndex < length) { + nums[slowIndex++] = 0; + } +}; +``` + + + -----------------------
diff --git "a/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" "b/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" index ffa66c0258..41c6a7cef4 100644 --- "a/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" @@ -6,7 +6,7 @@ ## 300.最长递增子序列 -[力扣题目链接](https://leetcode-cn.com/problems/longest-increasing-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/longest-increasing-subsequence/) 给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。 diff --git "a/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" "b/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" index f037fe8558..229fc636d1 100644 --- "a/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" +++ "b/problems/0309.346円234円200円344円275円263円344円271円260円345円215円226円350円202円241円347円245円250円346円227円266円346円234円272円345円220円253円345円206円267円345円206円273円346円234円237円.md" @@ -6,7 +6,7 @@ ## 309.最佳买卖股票时机含冷冻期 -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/) 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。 diff --git "a/problems/0322.351円233円266円351円222円261円345円205円221円346円215円242円.md" "b/problems/0322.351円233円266円351円222円261円345円205円221円346円215円242円.md" index fc0490c8df..03c6a4e228 100644 --- "a/problems/0322.351円233円266円351円222円261円345円205円221円346円215円242円.md" +++ "b/problems/0322.351円233円266円351円222円261円345円205円221円346円215円242円.md" @@ -7,7 +7,7 @@ ## 322. 零钱兑换 -[力扣题目链接](https://leetcode-cn.com/problems/coin-change/) +[力扣题目链接](https://leetcode.cn/problems/coin-change/) 给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。 diff --git "a/problems/0332.351円207円215円346円226円260円345円256円211円346円216円222円350円241円214円347円250円213円.md" "b/problems/0332.351円207円215円346円226円260円345円256円211円346円216円222円350円241円214円347円250円213円.md" index c71b2a9357..71942c799d 100644 --- "a/problems/0332.351円207円215円346円226円260円345円256円211円346円216円222円350円241円214円347円250円213円.md" +++ "b/problems/0332.351円207円215円346円226円260円345円256円211円346円216円222円350円241円214円347円250円213円.md" @@ -9,7 +9,7 @@ # 332.重新安排行程 -[力扣题目链接](https://leetcode-cn.com/problems/reconstruct-itinerary/) +[力扣题目链接](https://leetcode.cn/problems/reconstruct-itinerary/) 给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。 diff --git "a/problems/0337.346円211円223円345円256円266円345円212円253円350円210円215円III.md" "b/problems/0337.346円211円223円345円256円266円345円212円253円350円210円215円III.md" index 6f50723d8f..d2add23256 100644 --- "a/problems/0337.346円211円223円345円256円266円345円212円253円350円210円215円III.md" +++ "b/problems/0337.346円211円223円345円256円266円345円212円253円350円210円215円III.md" @@ -7,7 +7,7 @@ ## 337.打家劫舍 III -[力扣题目链接](https://leetcode-cn.com/problems/house-robber-iii/) +[力扣题目链接](https://leetcode.cn/problems/house-robber-iii/) 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为"根"。 除了"根"之外,每栋房子有且只有一个"父"房子与之相连。一番侦察之后,聪明的小偷意识到"这个地方的所有房屋的排列类似于一棵二叉树"。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 diff --git "a/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" "b/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" index 279f1d7182..dd03937f3e 100644 --- "a/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" +++ "b/problems/0343.346円225円264円346円225円260円346円213円206円345円210円206円.md" @@ -6,7 +6,7 @@ # 343. 整数拆分 -[力扣题目链接](https://leetcode-cn.com/problems/integer-break/) +[力扣题目链接](https://leetcode.cn/problems/integer-break/) 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。 diff --git "a/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md" "b/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md" index e1f27bd7c5..aba6e2f381 100644 --- "a/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0344.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円.md" @@ -10,7 +10,7 @@ # 344.反转字符串 -[力扣题目链接](https://leetcode-cn.com/problems/reverse-string/) +[力扣题目链接](https://leetcode.cn/problems/reverse-string/) 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 @@ -266,6 +266,38 @@ public class Solution } } ``` + + +PHP: +```php +// 双指针 +// 一: +function reverseString(&$s) { + $left = 0; + $right = count($s)-1; + while($left<$right){ + $temp = $s[$left]; + $s[$left] = $s[$right]; + $s[$right] = $temp; + $left++; + $right--; + } +} + +// 二: +function reverseString(&$s) { + $this->reverse($s,0,count($s)-1); +} +// 按指定位置交换元素 +function reverse(&$s, $start, $end) { + for ($i = $start, $j = $end; $i < $j; $i++, $j--) { + $tmp = $s[$i]; + $s[$i] = $s[$j]; + $s[$j] = $tmp; + } + } +``` + Scala: ```scala object Solution { diff --git "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" index 20932b2814..a04041ccdf 100644 --- "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" +++ "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" @@ -11,7 +11,7 @@ # 347.前 K 个高频元素 -[力扣题目链接](https://leetcode-cn.com/problems/top-k-frequent-elements/) +[力扣题目链接](https://leetcode.cn/problems/top-k-frequent-elements/) 给定一个非空的整数数组,返回其中出现频率前 k 高的元素。 diff --git "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" index 4fbdd41460..9851864750 100644 --- "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" +++ "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" @@ -12,7 +12,7 @@ ## 349. 两个数组的交集 -[力扣题目链接](https://leetcode-cn.com/problems/intersection-of-two-arrays/) +[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/) 题意:给定两个数组,编写一个函数来计算它们的交集。 diff --git "a/problems/0376.346円221円206円345円212円250円345円272円217円345円210円227円.md" "b/problems/0376.346円221円206円345円212円250円345円272円217円345円210円227円.md" index 6822896e73..00f8f70cf9 100644 --- "a/problems/0376.346円221円206円345円212円250円345円272円217円345円210円227円.md" +++ "b/problems/0376.346円221円206円345円212円250円345円272円217円345円210円227円.md" @@ -9,7 +9,7 @@ # 376. 摆动序列 -[力扣题目链接](https://leetcode-cn.com/problems/wiggle-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/wiggle-subsequence/) 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。 diff --git "a/problems/0377.347円273円204円345円220円210円346円200円273円345円222円214円342円205円243円.md" "b/problems/0377.347円273円204円345円220円210円346円200円273円345円222円214円342円205円243円.md" index 1d808a3a0a..f8e544a927 100644 --- "a/problems/0377.347円273円204円345円220円210円346円200円273円345円222円214円342円205円243円.md" +++ "b/problems/0377.347円273円204円345円220円210円346円200円273円345円222円214円342円205円243円.md" @@ -8,7 +8,7 @@ ## 377. 组合总和 IV -[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-iv/) +[力扣题目链接](https://leetcode.cn/problems/combination-sum-iv/) 难度:中等 diff --git "a/problems/0383.350円265円216円351円207円221円344円277円241円.md" "b/problems/0383.350円265円216円351円207円221円344円277円241円.md" index 9c3dda8c4c..3cde547260 100644 --- "a/problems/0383.350円265円216円351円207円221円344円277円241円.md" +++ "b/problems/0383.350円265円216円351円207円221円344円277円241円.md" @@ -10,7 +10,7 @@ # 383. 赎金信 -[力扣题目链接](https://leetcode-cn.com/problems/ransom-note/) +[力扣题目链接](https://leetcode.cn/problems/ransom-note/) 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。 diff --git "a/problems/0392.345円210円244円346円226円255円345円255円220円345円272円217円345円210円227円.md" "b/problems/0392.345円210円244円346円226円255円345円255円220円345円272円217円345円210円227円.md" index 3f7eb11d8d..9a26d63900 100644 --- "a/problems/0392.345円210円244円346円226円255円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0392.345円210円244円346円226円255円345円255円220円345円272円217円345円210円227円.md" @@ -7,7 +7,7 @@ ## 392.判断子序列 -[力扣题目链接](https://leetcode-cn.com/problems/is-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/is-subsequence/) 给定字符串 s 和 t ,判断 s 是否为 t 的子序列。 diff --git "a/problems/0404.345円267円246円345円217円266円345円255円220円344円271円213円345円222円214円.md" "b/problems/0404.345円267円246円345円217円266円345円255円220円344円271円213円345円222円214円.md" index 4ef68d3b4e..8485cdacf5 100644 --- "a/problems/0404.345円267円246円345円217円266円345円255円220円344円271円213円345円222円214円.md" +++ "b/problems/0404.345円267円246円345円217円266円345円255円220円344円271円213円345円222円214円.md" @@ -7,7 +7,7 @@ # 404.左叶子之和 -[力扣题目链接](https://leetcode-cn.com/problems/sum-of-left-leaves/) +[力扣题目链接](https://leetcode.cn/problems/sum-of-left-leaves/) 计算给定二叉树的所有左叶子之和。 @@ -516,6 +516,44 @@ int sumOfLeftLeaves(struct TreeNode* root){ } ``` +## Scala + +**递归:** +```scala +object Solution { + def sumOfLeftLeaves(root: TreeNode): Int = { + if(root == null) return 0 + var midValue = 0 + if(root.left != null && root.left.left == null && root.left.right == null){ + midValue = root.left.value + } + // return关键字可以省略 + midValue + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right) + } +} +``` + +**迭代:** +```scala +object Solution { + import scala.collection.mutable + def sumOfLeftLeaves(root: TreeNode): Int = { + val stack = mutable.Stack[TreeNode]() + if (root == null) return 0 + stack.push(root) + var sum = 0 + while (!stack.isEmpty) { + val curNode = stack.pop() + if (curNode.left != null && curNode.left.left == null && curNode.left.right == null) { + sum += curNode.left.value // 如果满足条件就累加 + } + if (curNode.right != null) stack.push(curNode.right) + if (curNode.left != null) stack.push(curNode.left) + } + sum + } +} +``` -----------------------
diff --git "a/problems/0406.346円240円271円346円215円256円350円272円253円351円253円230円351円207円215円345円273円272円351円230円237円345円210円227円.md" "b/problems/0406.346円240円271円346円215円256円350円272円253円351円253円230円351円207円215円345円273円272円351円230円237円345円210円227円.md" index 641086a93c..879820eae9 100644 --- "a/problems/0406.346円240円271円346円215円256円350円272円253円351円253円230円351円207円215円345円273円272円351円230円237円345円210円227円.md" +++ "b/problems/0406.346円240円271円346円215円256円350円272円253円351円253円230円351円207円215円345円273円272円351円230円237円345円210円227円.md" @@ -7,7 +7,7 @@ # 406.根据身高重建队列 -[力扣题目链接](https://leetcode-cn.com/problems/queue-reconstruction-by-height/) +[力扣题目链接](https://leetcode.cn/problems/queue-reconstruction-by-height/) 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。 diff --git "a/problems/0416.345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" "b/problems/0416.345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" index 7347cd6fa9..5419fc1f1f 100644 --- "a/problems/0416.345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" +++ "b/problems/0416.345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" @@ -6,7 +6,7 @@ ## 416. 分割等和子集 -[力扣题目链接](https://leetcode-cn.com/problems/partition-equal-subset-sum/) +[力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) 题目难易:中等 diff --git "a/problems/0435.346円227円240円351円207円215円345円217円240円345円214円272円351円227円264円.md" "b/problems/0435.346円227円240円351円207円215円345円217円240円345円214円272円351円227円264円.md" index 66aa124475..f1e259aefa 100644 --- "a/problems/0435.346円227円240円351円207円215円345円217円240円345円214円272円351円227円264円.md" +++ "b/problems/0435.346円227円240円351円207円215円345円217円240円345円214円272円351円227円264円.md" @@ -7,7 +7,7 @@ # 435. 无重叠区间 -[力扣题目链接](https://leetcode-cn.com/problems/non-overlapping-intervals/) +[力扣题目链接](https://leetcode.cn/problems/non-overlapping-intervals/) 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。 diff --git "a/problems/0450.345円210円240円351円231円244円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円350円212円202円347円202円271円.md" "b/problems/0450.345円210円240円351円231円244円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円350円212円202円347円202円271円.md" index e8f7e54c4b..aca9084fa6 100644 --- "a/problems/0450.345円210円240円351円231円244円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円350円212円202円347円202円271円.md" +++ "b/problems/0450.345円210円240円351円231円244円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円350円212円202円347円202円271円.md" @@ -9,7 +9,7 @@ # 450.删除二叉搜索树中的节点 -[力扣题目链接]( https://leetcode-cn.com/problems/delete-node-in-a-bst/) +[力扣题目链接]( https://leetcode.cn/problems/delete-node-in-a-bst/) 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。 diff --git "a/problems/0452.347円224円250円346円234円200円345円260円221円346円225円260円351円207円217円347円232円204円347円256円255円345円274円225円347円210円206円346円260円224円347円220円203円.md" "b/problems/0452.347円224円250円346円234円200円345円260円221円346円225円260円351円207円217円347円232円204円347円256円255円345円274円225円347円210円206円346円260円224円347円220円203円.md" index d4bbe961ee..e593b87672 100644 --- "a/problems/0452.347円224円250円346円234円200円345円260円221円346円225円260円351円207円217円347円232円204円347円256円255円345円274円225円347円210円206円346円260円224円347円220円203円.md" +++ "b/problems/0452.347円224円250円346円234円200円345円260円221円346円225円260円351円207円217円347円232円204円347円256円255円345円274円225円347円210円206円346円260円224円347円220円203円.md" @@ -7,7 +7,7 @@ # 452. 用最少数量的箭引爆气球 -[力扣题目链接](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/) +[力扣题目链接](https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/) 在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了。开始坐标总是小于结束坐标。 diff --git "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" index 726fdb1569..d22e23359d 100644 --- "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" +++ "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" @@ -9,7 +9,7 @@ # 第454题.四数相加II -[力扣题目链接](https://leetcode-cn.com/problems/4sum-ii/) +[力扣题目链接](https://leetcode.cn/problems/4sum-ii/) 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。 diff --git "a/problems/0455.345円210円206円345円217円221円351円245円274円345円271円262円.md" "b/problems/0455.345円210円206円345円217円221円351円245円274円345円271円262円.md" index 17db4a859e..b787dee6be 100644 --- "a/problems/0455.345円210円206円345円217円221円351円245円274円345円271円262円.md" +++ "b/problems/0455.345円210円206円345円217円221円351円245円274円345円271円262円.md" @@ -7,7 +7,7 @@ # 455.分发饼干 -[力扣题目链接](https://leetcode-cn.com/problems/assign-cookies/) +[力扣题目链接](https://leetcode.cn/problems/assign-cookies/) 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 diff --git "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" index a51c68eea7..bb55bd7ce0 100644 --- "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" @@ -11,7 +11,7 @@ # 459.重复的子字符串 -[力扣题目链接](https://leetcode-cn.com/problems/repeated-substring-pattern/) +[力扣题目链接](https://leetcode.cn/problems/repeated-substring-pattern/) 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 diff --git "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" index 9911dfe5a3..ea0381402d 100644 --- "a/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" +++ "b/problems/0463.345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 463. 岛屿的周长 -[力扣题目链接](https://leetcode-cn.com/problems/island-perimeter/) +[力扣题目链接](https://leetcode.cn/problems/island-perimeter/) ## 思路 diff --git "a/problems/0474.344円270円200円345円222円214円351円233円266円.md" "b/problems/0474.344円270円200円345円222円214円351円233円266円.md" index d38ce03f89..a5018baf21 100644 --- "a/problems/0474.344円270円200円345円222円214円351円233円266円.md" +++ "b/problems/0474.344円270円200円345円222円214円351円233円266円.md" @@ -7,7 +7,7 @@ ## 474.一和零 -[力扣题目链接](https://leetcode-cn.com/problems/ones-and-zeroes/) +[力扣题目链接](https://leetcode.cn/problems/ones-and-zeroes/) 给你一个二进制字符串数组 strs 和两个整数 m 和 n 。 diff --git "a/problems/0491.351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円.md" "b/problems/0491.351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円.md" index 3ea2382b1f..291a19bd5e 100644 --- "a/problems/0491.351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0491.351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円.md" @@ -9,7 +9,7 @@ # 491.递增子序列 -[力扣题目链接](https://leetcode-cn.com/problems/increasing-subsequences/) +[力扣题目链接](https://leetcode.cn/problems/increasing-subsequences/) 给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。 diff --git "a/problems/0494.347円233円256円346円240円207円345円222円214円.md" "b/problems/0494.347円233円256円346円240円207円345円222円214円.md" index 0cdae393b0..8f116fae8d 100644 --- "a/problems/0494.347円233円256円346円240円207円345円222円214円.md" +++ "b/problems/0494.347円233円256円346円240円207円345円222円214円.md" @@ -7,7 +7,7 @@ ## 494. 目标和 -[力扣题目链接](https://leetcode-cn.com/problems/target-sum/) +[力扣题目链接](https://leetcode.cn/problems/target-sum/) 难度:中等 diff --git "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" index 274cc32b58..3c94881542 100644 --- "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" +++ "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" @@ -6,7 +6,7 @@ # 496.下一个更大元素 I -[力扣题目链接](https://leetcode-cn.com/problems/next-greater-element-i/) +[力扣题目链接](https://leetcode.cn/problems/next-greater-element-i/) 给你两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。 diff --git "a/problems/0501.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円344円274円227円346円225円260円.md" "b/problems/0501.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円344円274円227円346円225円260円.md" index 9cb5d0713a..671d8061fa 100644 --- "a/problems/0501.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円344円274円227円346円225円260円.md" +++ "b/problems/0501.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円344円274円227円346円225円260円.md" @@ -9,7 +9,9 @@ # 501.二叉搜索树中的众数 -[力扣题目链接](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/) + +[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/) + 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。 @@ -798,7 +800,76 @@ function findMode(root: TreeNode | null): number[] { }; ``` +## Scala + +暴力: +```scala +object Solution { + // 导包 + import scala.collection.mutable // 集合包 + import scala.util.control.Breaks.{break, breakable} // 流程控制包 + def findMode(root: TreeNode): Array[Int] = { + var map = mutable.HashMap[Int, Int]() // 存储节点的值,和该值出现的次数 + def searchBST(curNode: TreeNode): Unit = { + if (curNode == null) return + var value = map.getOrElse(curNode.value, 0) + map.put(curNode.value, value + 1) + searchBST(curNode.left) + searchBST(curNode.right) + } + searchBST(root) // 前序遍历把每个节点的值加入到里面 + // 将map转换为list,随后根据元组的第二个值进行排序 + val list = map.toList.sortWith((map1, map2) => { + if (map1._2> map2._2) true else false + }) + var res = mutable.ArrayBuffer[Int]() + res.append(list(0)._1) // 将第一个加入结果集 + breakable { + for (i <- 1 until list.size) { + // 如果值相同就加入结果集合,反之break + if (list(i)._2 == list(0)._2) res.append(list(i)._1) + else break + } + } + res.toArray // 最终返回res的Array格式,return关键字可以省略 + } +} +``` +递归(利用二叉搜索树的性质): +```scala +object Solution { + import scala.collection.mutable + def findMode(root: TreeNode): Array[Int] = { + var maxCount = 0 // 最大频率 + var count = 0 // 统计频率 + var pre: TreeNode = null + var result = mutable.ArrayBuffer[Int]() + + def searchBST(cur: TreeNode): Unit = { + if (cur == null) return + searchBST(cur.left) + if (pre == null) count = 1 // 等于空置为1 + else if (pre.value == cur.value) count += 1 // 与上一个节点的值相同加1 + else count = 1 // 与上一个节点的值不同 + pre = cur + + // 如果和最大值相同,则放入结果集 + if (count == maxCount) result.append(cur.value) + + // 如果当前计数大于最大值频率,更新最大值,清空结果集 + if (count> maxCount) { + maxCount = count + result.clear() + result.append(cur.value) + } + searchBST(cur.right) + } + searchBST(root) + result.toArray // return关键字可以省略 + } +} +``` ----------------------- diff --git "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" index 33807d265d..0bd0fb82ef 100644 --- "a/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" +++ "b/problems/0503.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円II.md" @@ -6,7 +6,7 @@ # 503.下一个更大元素II -[力扣题目链接](https://leetcode-cn.com/problems/next-greater-element-ii/) +[力扣题目链接](https://leetcode.cn/problems/next-greater-element-ii/) 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。 diff --git "a/problems/0509.346円226円220円346円263円242円351円202円243円345円245円221円346円225円260円.md" "b/problems/0509.346円226円220円346円263円242円351円202円243円345円245円221円346円225円260円.md" index 1d17784d43..785d0125b7 100644 --- "a/problems/0509.346円226円220円346円263円242円351円202円243円345円245円221円346円225円260円.md" +++ "b/problems/0509.346円226円220円346円263円242円351円202円243円345円245円221円346円225円260円.md" @@ -6,7 +6,7 @@ # 509. 斐波那契数 -[力扣题目链接](https://leetcode-cn.com/problems/fibonacci-number/) +[力扣题目链接](https://leetcode.cn/problems/fibonacci-number/) 斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) = 0,F(1) = 1 @@ -234,6 +234,7 @@ func fib(n int) int { } ``` ### Javascript +解法一 ```Javascript var fib = function(n) { let dp = [0, 1] @@ -244,6 +245,23 @@ var fib = function(n) { return dp[n] }; ``` +解法二:时间复杂度O(N),空间复杂度O(1) +```Javascript +var fib = function(n) { + // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1) + let pre1 = 1 + let pre2 = 0 + let temp + if (n === 0) return 0 + if (n === 1) return 1 + for(let i = 2; i <= n; i++) { + temp = pre1 + pre1 = pre1 + pre2 + pre2 = temp + } + return pre1 +}; +``` TypeScript diff --git "a/problems/0513.346円211円276円346円240円221円345円267円246円344円270円213円350円247円222円347円232円204円345円200円274円.md" "b/problems/0513.346円211円276円346円240円221円345円267円246円344円270円213円350円247円222円347円232円204円345円200円274円.md" index 12c62c7068..817c22c9cc 100644 --- "a/problems/0513.346円211円276円346円240円221円345円267円246円344円270円213円350円247円222円347円232円204円345円200円274円.md" +++ "b/problems/0513.346円211円276円346円240円221円345円267円246円344円270円213円350円247円222円347円232円204円345円200円274円.md" @@ -7,7 +7,7 @@ # 513.找树左下角的值 -[力扣题目链接](https://leetcode-cn.com/problems/find-bottom-left-tree-value/) +[力扣题目链接](https://leetcode.cn/problems/find-bottom-left-tree-value/) 给定一个二叉树,在树的最后一行找到最左边的值。 @@ -546,7 +546,50 @@ func findBottomLeftValue(_ root: TreeNode?) -> Int { } ``` +## Scala + +递归版本: +```scala +object Solution { + def findBottomLeftValue(root: TreeNode): Int = { + var maxLeftValue = 0 + var maxLen = Int.MinValue + // 递归方法 + def traversal(node: TreeNode, leftLen: Int): Unit = { + // 如果左右都为空并且当前深度大于最大深度,记录最左节点的值 + if (node.left == null && node.right == null && leftLen> maxLen) { + maxLen = leftLen + maxLeftValue = node.value + } + if (node.left != null) traversal(node.left, leftLen + 1) + if (node.right != null) traversal(node.right, leftLen + 1) + } + traversal(root, 0) // 调用方法 + maxLeftValue // return关键字可以省略 + } +} +``` +层序遍历: +```scala + import scala.collection.mutable + + def findBottomLeftValue(root: TreeNode): Int = { + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + var res = 0 // 记录每层最左侧结果 + while (!queue.isEmpty) { + val len = queue.size + for (i <- 0 until len) { + val curNode = queue.dequeue() + if (i == 0) res = curNode.value // 记录最最左侧的节点 + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + } + res // 最终返回结果,return关键字可以省略 + } +``` -----------------------
diff --git "a/problems/0516.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円345円272円217円345円210円227円.md" "b/problems/0516.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円345円272円217円345円210円227円.md" index 33de8fd165..60d5e9205b 100644 --- "a/problems/0516.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0516.346円234円200円351円225円277円345円233円236円346円226円207円345円255円220円345円272円217円345円210円227円.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

## 516.最长回文子序列 -[力扣题目链接](https://leetcode-cn.com/problems/longest-palindromic-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/longest-palindromic-subsequence/) 给定一个字符串 s ,找到其中最长的回文子序列,并返回该序列的长度。可以假设 s 的最大长度为 1000 。 diff --git "a/problems/0518.351円233円266円351円222円261円345円205円221円346円215円242円II.md" "b/problems/0518.351円233円266円351円222円261円345円205円221円346円215円242円II.md" index b6593438c8..222a10d759 100644 --- "a/problems/0518.351円233円266円351円222円261円345円205円221円346円215円242円II.md" +++ "b/problems/0518.351円233円266円351円222円261円345円205円221円346円215円242円II.md" @@ -7,7 +7,7 @@ ## 518. 零钱兑换 II -[力扣题目链接](https://leetcode-cn.com/problems/coin-change-2/) +[力扣题目链接](https://leetcode.cn/problems/coin-change-2/) 难度:中等 diff --git "a/problems/0530.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円345円260円217円347円273円235円345円257円271円345円267円256円.md" "b/problems/0530.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円345円260円217円347円273円235円345円257円271円345円267円256円.md" index 77699c9f4e..809f500bff 100644 --- "a/problems/0530.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円345円260円217円347円273円235円345円257円271円345円267円256円.md" +++ "b/problems/0530.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円347円232円204円346円234円200円345円260円217円347円273円235円345円257円271円345円267円256円.md" @@ -9,7 +9,7 @@ # 530.二叉搜索树的最小绝对差 -[力扣题目链接](https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/) +[力扣题目链接](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/) 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。 @@ -431,7 +431,84 @@ function getMinimumDifference(root: TreeNode | null): number { }; ``` +## Scala +构建二叉树的有序数组: + +```scala +object Solution { + import scala.collection.mutable + def getMinimumDifference(root: TreeNode): Int = { + val arr = mutable.ArrayBuffer[Int]() + def traversal(node: TreeNode): Unit = { + if (node == null) return + traversal(node.left) + arr.append(node.value) + traversal(node.right) + } + traversal(root) + // 在有序数组上求最小差值 + var result = Int.MaxValue + for (i <- 1 until arr.size) { + result = math.min(result, arr(i) - arr(i - 1)) + } + result // 返回最小差值 + } +} +``` + +递归记录前一个节点: + +```scala +object Solution { + def getMinimumDifference(root: TreeNode): Int = { + var result = Int.MaxValue // 初始化为最大值 + var pre: TreeNode = null // 记录前一个节点 + + def traversal(cur: TreeNode): Unit = { + if (cur == null) return + traversal(cur.left) + if (pre != null) { + // 对比result与节点之间的差值 + result = math.min(result, cur.value - pre.value) + } + pre = cur + traversal(cur.right) + } + + traversal(root) + result // return关键字可以省略 + } +} +``` + +迭代解决: + +```scala +object Solution { + import scala.collection.mutable + def getMinimumDifference(root: TreeNode): Int = { + var result = Int.MaxValue // 初始化为最大值 + var pre: TreeNode = null // 记录前一个节点 + var cur = root + var stack = mutable.Stack[TreeNode]() + while (cur != null || !stack.isEmpty) { + if (cur != null) { + stack.push(cur) + cur = cur.left + } else { + cur = stack.pop() + if (pre != null) { + result = math.min(result, cur.value - pre.value) + } + pre = cur + cur = cur.right + } + } + result // return关键字可以省略 + } +} +``` -----------------------
diff --git "a/problems/0538.346円212円212円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円350円275円254円346円215円242円344円270円272円347円264円257円345円212円240円346円240円221円.md" "b/problems/0538.346円212円212円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円350円275円254円346円215円242円344円270円272円347円264円257円345円212円240円346円240円221円.md" index 37eb7d0f2c..853cca6f37 100644 --- "a/problems/0538.346円212円212円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円350円275円254円346円215円242円344円270円272円347円264円257円345円212円240円346円240円221円.md" +++ "b/problems/0538.346円212円212円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円350円275円254円346円215円242円344円270円272円347円264円257円345円212円240円346円240円221円.md" @@ -7,7 +7,7 @@ # 538.把二叉搜索树转换为累加树 -[力扣题目链接](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/) +[力扣题目链接](https://leetcode.cn/problems/convert-bst-to-greater-tree/) 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 diff --git "a/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md" "b/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md" index 99d6ebe04e..84061ef56b 100644 --- "a/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md" +++ "b/problems/0541.345円217円215円350円275円254円345円255円227円347円254円246円344円270円262円II.md" @@ -10,7 +10,7 @@ # 541. 反转字符串II -[力扣题目链接](https://leetcode-cn.com/problems/reverse-string-ii/) +[力扣题目链接](https://leetcode.cn/problems/reverse-string-ii/) 给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。 @@ -53,10 +53,10 @@ public: // 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符 if (i + k <= s.size()) { reverse(s.begin() + i, s.begin() + i + k ); - continue; + } else { + // 3. 剩余字符少于 k 个,则将剩余字符全部反转。 + reverse(s.begin() + i, s.end()); } - // 3. 剩余字符少于 k 个,则将剩余字符全部反转。 - reverse(s.begin() + i, s.begin() + s.size()); } return s; } diff --git "a/problems/0583.344円270円244円344円270円252円345円255円227円347円254円246円344円270円262円347円232円204円345円210円240円351円231円244円346円223円215円344円275円234円.md" "b/problems/0583.344円270円244円344円270円252円345円255円227円347円254円246円344円270円262円347円232円204円345円210円240円351円231円244円346円223円215円344円275円234円.md" index 00f1170009..fc7e6f3940 100644 --- "a/problems/0583.344円270円244円344円270円252円345円255円227円347円254円246円344円270円262円347円232円204円345円210円240円351円231円244円346円223円215円344円275円234円.md" +++ "b/problems/0583.344円270円244円344円270円252円345円255円227円347円254円246円344円270円262円347円232円204円345円210円240円351円231円244円346円223円215円344円275円234円.md" @@ -6,7 +6,7 @@ ## 583. 两个字符串的删除操作 -[力扣题目链接](https://leetcode-cn.com/problems/delete-operation-for-two-strings/) +[力扣题目链接](https://leetcode.cn/problems/delete-operation-for-two-strings/) 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。 diff --git "a/problems/0617.345円220円210円345円271円266円344円272円214円345円217円211円346円240円221円.md" "b/problems/0617.345円220円210円345円271円266円344円272円214円345円217円211円346円240円221円.md" index 55786ea988..acdcc0aa41 100644 --- "a/problems/0617.345円220円210円345円271円266円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0617.345円220円210円345円271円266円344円272円214円345円217円211円346円240円221円.md" @@ -7,7 +7,7 @@ # 617.合并二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/merge-two-binary-trees/) +[力扣题目链接](https://leetcode.cn/problems/merge-two-binary-trees/) 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。 @@ -631,7 +631,60 @@ function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode | }; ``` +## Scala + +递归: +```scala +object Solution { + def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = { + if (root1 == null) return root2 // 如果root1为空,返回root2 + if (root2 == null) return root1 // 如果root2为空,返回root1 + // 新建一个节点,值为两个节点的和 + var node = new TreeNode(root1.value + root2.value) + // 往下递归 + node.left = mergeTrees(root1.left, root2.left) + node.right = mergeTrees(root1.right, root2.right) + node // 返回node,return关键字可以省略 + } +} +``` +迭代: +```scala +object Solution { + import scala.collection.mutable + def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = { + if (root1 == null) return root2 + if (root2 == null) return root1 + var stack = mutable.Stack[TreeNode]() + // 先放node2再放node1 + stack.push(root2) + stack.push(root1) + while (!stack.isEmpty) { + var node1 = stack.pop() + var node2 = stack.pop() + node1.value += node2.value + if (node1.right != null && node2.right != null) { + stack.push(node2.right) + stack.push(node1.right) + } else { + if(node1.right == null){ + node1.right = node2.right + } + } + if (node1.left != null && node2.left != null) { + stack.push(node2.left) + stack.push(node1.left) + } else { + if(node1.left == null){ + node1.left = node2.left + } + } + } + root1 + } +} +``` ----------------------- diff --git "a/problems/0647.345円233円236円346円226円207円345円255円220円344円270円262円.md" "b/problems/0647.345円233円236円346円226円207円345円255円220円344円270円262円.md" index 6045ba7b49..c0b34e8a8b 100644 --- "a/problems/0647.345円233円236円346円226円207円345円255円220円344円270円262円.md" +++ "b/problems/0647.345円233円236円346円226円207円345円255円220円344円270円262円.md" @@ -6,7 +6,7 @@ ## 647. 回文子串 -[力扣题目链接](https://leetcode-cn.com/problems/palindromic-substrings/) +[力扣题目链接](https://leetcode.cn/problems/palindromic-substrings/) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。 diff --git "a/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" "b/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" index 6e84c9fde2..f1b3be11c3 100644 --- "a/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" +++ "b/problems/0649.Dota2345円217円202円350円256円256円351円231円242円.md" @@ -8,7 +8,7 @@ # 649. Dota2 参议院 -[力扣题目链接](https://leetcode-cn.com/problems/dota2-senate/) +[力扣题目链接](https://leetcode.cn/problems/dota2-senate/) Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) diff --git "a/problems/0654.346円234円200円345円244円247円344円272円214円345円217円211円346円240円221円.md" "b/problems/0654.346円234円200円345円244円247円344円272円214円345円217円211円346円240円221円.md" index 1c73354b00..e2a64bcd9b 100644 --- "a/problems/0654.346円234円200円345円244円247円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0654.346円234円200円345円244円247円344円272円214円345円217円211円346円240円221円.md" @@ -7,7 +7,7 @@ # 654.最大二叉树 -[力扣题目地址](https://leetcode-cn.com/problems/maximum-binary-tree/) +[力扣题目地址](https://leetcode.cn/problems/maximum-binary-tree/) 给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下: @@ -476,7 +476,33 @@ func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? { } ``` +## Scala + +```scala +object Solution { + def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = { + if (nums.size == 0) return null + // 找到数组最大值 + var maxIndex = 0 + var maxValue = Int.MinValue + for (i <- nums.indices) { + if (nums(i)> maxValue) { + maxIndex = i + maxValue = nums(i) + } + } + + // 构建一棵树 + var root = new TreeNode(maxValue, null, null) + + // 递归寻找左右子树 + root.left = constructMaximumBinaryTree(nums.slice(0, maxIndex)) + root.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1, nums.length)) + root // 返回root + } +} +``` -----------------------
diff --git "a/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" "b/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" index 4eb69a6ce3..3d91a5c313 100644 --- "a/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" +++ "b/problems/0657.346円234円272円345円231円250円344円272円272円350円203円275円345円220円246円350円277円224円345円233円236円345円216円237円347円202円271円.md" @@ -7,7 +7,7 @@ # 657. 机器人能否返回原点 -[力扣题目链接](https://leetcode-cn.com/problems/robot-return-to-origin/) +[力扣题目链接](https://leetcode.cn/problems/robot-return-to-origin/) 在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。 diff --git "a/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" "b/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" index 385b226863..154ba5a9a6 100644 --- "a/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" +++ "b/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" @@ -10,7 +10,7 @@ # 669. 修剪二叉搜索树 -[力扣题目链接](https://leetcode-cn.com/problems/trim-a-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/trim-a-binary-search-tree/) 给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。 diff --git "a/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" "b/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" index 9a2c5db29d..8a6a2d4658 100644 --- "a/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" +++ "b/problems/0673.346円234円200円351円225円277円351円200円222円345円242円236円345円255円220円345円272円217円345円210円227円347円232円204円344円270円252円346円225円260円.md" @@ -8,7 +8,7 @@ # 673.最长递增子序列的个数 -[力扣题目链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/number-of-longest-increasing-subsequence/) 给定一个未排序的整数数组,找到最长递增子序列的个数。 diff --git "a/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" "b/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" index b1ed80c3e9..5865a68dac 100644 --- "a/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" +++ "b/problems/0674.346円234円200円351円225円277円350円277円236円347円273円255円351円200円222円345円242円236円345円272円217円345円210円227円.md" @@ -6,7 +6,7 @@ ## 674. 最长连续递增序列 -[力扣题目链接](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/longest-continuous-increasing-subsequence/) 给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。 diff --git "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" index a16833bc8e..3928e05106 100644 --- "a/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" +++ "b/problems/0684.345円206円227円344円275円231円350円277円236円346円216円245円.md" @@ -9,7 +9,7 @@ # 684.冗余连接 -[力扣题目链接](https://leetcode-cn.com/problems/redundant-connection/) +[力扣题目链接](https://leetcode.cn/problems/redundant-connection/) 树可以看成是一个连通且 无环 的 无向 图。 diff --git "a/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" "b/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" index d96d4912fe..a8da112449 100644 --- "a/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" +++ "b/problems/0685.345円206円227円344円275円231円350円277円236円346円216円245円II.md" @@ -7,7 +7,7 @@ # 685.冗余连接II -[力扣题目链接](https://leetcode-cn.com/problems/redundant-connection-ii/) +[力扣题目链接](https://leetcode.cn/problems/redundant-connection-ii/) 在本问题中,有根树指满足以下条件的 有向 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。 diff --git "a/problems/0700.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円220円234円347円264円242円.md" "b/problems/0700.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円220円234円347円264円242円.md" index 40cf4ea1fc..a8dcc69fc6 100644 --- "a/problems/0700.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円220円234円347円264円242円.md" +++ "b/problems/0700.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円220円234円347円264円242円.md" @@ -7,7 +7,7 @@ # 700.二叉搜索树中的搜索 -[力扣题目地址](https://leetcode-cn.com/problems/search-in-a-binary-search-tree/) +[力扣题目地址](https://leetcode.cn/problems/search-in-a-binary-search-tree/) 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。 @@ -363,7 +363,34 @@ function searchBST(root: TreeNode | null, val: number): TreeNode | null { }; ``` +## Scala +递归: +```scala +object Solution { + def searchBST(root: TreeNode, value: Int): TreeNode = { + if (root == null || value == root.value) return root + // 相当于三元表达式,在Scala中if...else有返回值 + if (value < root.value) searchBST(root.left, value) else searchBST(root.right, value) + } +} +``` + +迭代: +```scala +object Solution { + def searchBST(root: TreeNode, value: Int): TreeNode = { + // 因为root是不可变量,所以需要赋值给一个可变量 + var node = root + while (node != null) { + if (value < node.value) node = node.left + else if (value> node.value) node = node.right + else return node + } + null // 没有返回就返回空 + } +} +``` -----------------------
diff --git "a/problems/0701.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円217円222円345円205円245円346円223円215円344円275円234円.md" "b/problems/0701.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円217円222円345円205円245円346円223円215円344円275円234円.md" index 50e39ade5e..102f091e9c 100644 --- "a/problems/0701.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円217円222円345円205円245円346円223円215円344円275円234円.md" +++ "b/problems/0701.344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円344円270円255円347円232円204円346円217円222円345円205円245円346円223円215円344円275円234円.md" @@ -7,7 +7,7 @@ # 701.二叉搜索树中的插入操作 -[力扣题目链接](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/insert-into-a-binary-search-tree/) 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。 diff --git "a/problems/0704.344円272円214円345円210円206円346円237円245円346円211円276円.md" "b/problems/0704.344円272円214円345円210円206円346円237円245円346円211円276円.md" index 50f01226f0..6a37e4d107 100644 --- "a/problems/0704.344円272円214円345円210円206円346円237円245円346円211円276円.md" +++ "b/problems/0704.344円272円214円345円210円206円346円237円245円346円211円276円.md" @@ -7,7 +7,7 @@ ## 704. 二分查找 -[力扣题目链接](https://leetcode-cn.com/problems/binary-search/) +[力扣题目链接](https://leetcode.cn/problems/binary-search/) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 diff --git "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" index dcdb53f44e..6ee11eefbf 100644 --- "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" +++ "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" @@ -9,7 +9,7 @@ # 707.设计链表 -[力扣题目链接](https://leetcode-cn.com/problems/design-linked-list/) +[力扣题目链接](https://leetcode.cn/problems/design-linked-list/) 题意: diff --git "a/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円.md" "b/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円.md" index b27631c68c..7600c52cc0 100644 --- "a/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円.md" +++ "b/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円.md" @@ -7,7 +7,7 @@ # 714. 买卖股票的最佳时机含手续费 -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。 diff --git "a/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" "b/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" index 5625604b9d..7734450ec1 100644 --- "a/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" +++ "b/problems/0714.344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円272円345円220円253円346円211円213円347円273円255円350円264円271円357円274円210円345円212円250円346円200円201円350円247円204円345円210円222円357円274円211円.md" @@ -6,7 +6,7 @@ ## 714.买卖股票的最佳时机含手续费 -[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) +[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。 diff --git "a/problems/0718.346円234円200円351円225円277円351円207円215円345円244円215円345円255円220円346円225円260円347円273円204円.md" "b/problems/0718.346円234円200円351円225円277円351円207円215円345円244円215円345円255円220円346円225円260円347円273円204円.md" index 0b7b519958..18007b700b 100644 --- "a/problems/0718.346円234円200円351円225円277円351円207円215円345円244円215円345円255円220円346円225円260円347円273円204円.md" +++ "b/problems/0718.346円234円200円351円225円277円351円207円215円345円244円215円345円255円220円346円225円260円347円273円204円.md" @@ -6,7 +6,7 @@ ## 718. 最长重复子数组 -[力扣题目链接](https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/) +[力扣题目链接](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/) 给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。 diff --git "a/problems/0724.345円257円273円346円211円276円346円225円260円347円273円204円347円232円204円344円270円255円345円277円203円347円264円242円345円274円225円.md" "b/problems/0724.345円257円273円346円211円276円346円225円260円347円273円204円347円232円204円344円270円255円345円277円203円347円264円242円345円274円225円.md" index 14dcd2c08a..5a24a884f5 100644 --- "a/problems/0724.345円257円273円346円211円276円346円225円260円347円273円204円347円232円204円344円270円255円345円277円203円347円264円242円345円274円225円.md" +++ "b/problems/0724.345円257円273円346円211円276円346円225円260円347円273円204円347円232円204円344円270円255円345円277円203円347円264円242円345円274円225円.md" @@ -7,7 +7,7 @@ # 724.寻找数组的中心下标 -[力扣题目链接](https://leetcode-cn.com/problems/find-pivot-index/) +[力扣题目链接](https://leetcode.cn/problems/find-pivot-index/) 给你一个整数数组 nums ,请计算数组的 中心下标 。 @@ -140,6 +140,24 @@ var pivotIndex = function(nums) { }; ``` +### TypeScript + +```typescript +function pivotIndex(nums: number[]): number { + const length: number = nums.length; + const sum: number = nums.reduce((a, b) => a + b); + let leftSum: number = 0; + for (let i = 0; i < length; i++) { + const rightSum: number = sum - leftSum - nums[i]; + if (leftSum === rightSum) return i; + leftSum += nums[i]; + } + return -1; +}; +``` + + + -----------------------
diff --git "a/problems/0738.345円215円225円350円260円203円351円200円222円345円242円236円347円232円204円346円225円260円345円255円227円.md" "b/problems/0738.345円215円225円350円260円203円351円200円222円345円242円236円347円232円204円346円225円260円345円255円227円.md" index 4e4079a76e..d2f041f5fb 100644 --- "a/problems/0738.345円215円225円350円260円203円351円200円222円345円242円236円347円232円204円346円225円260円345円255円227円.md" +++ "b/problems/0738.345円215円225円350円260円203円351円200円222円345円242円236円347円232円204円346円225円260円345円255円227円.md" @@ -6,7 +6,7 @@ # 738.单调递增的数字 -[力扣题目链接](https://leetcode-cn.com/problems/monotone-increasing-digits/) +[力扣题目链接](https://leetcode.cn/problems/monotone-increasing-digits/) 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增。 diff --git "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" index 7472a3ae90..4605de0924 100644 --- "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" +++ "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" @@ -7,7 +7,7 @@ # 739. 每日温度 -[力扣题目链接](https://leetcode-cn.com/problems/daily-temperatures/) +[力扣题目链接](https://leetcode.cn/problems/daily-temperatures/) 请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。 diff --git "a/problems/0746.344円275円277円347円224円250円346円234円200円345円260円217円350円212円261円350円264円271円347円210円254円346円245円274円346円242円257円.md" "b/problems/0746.344円275円277円347円224円250円346円234円200円345円260円217円350円212円261円350円264円271円347円210円254円346円245円274円346円242円257円.md" index 5931fc8ac8..c2c737150e 100644 --- "a/problems/0746.344円275円277円347円224円250円346円234円200円345円260円217円350円212円261円350円264円271円347円210円254円346円245円274円346円242円257円.md" +++ "b/problems/0746.344円275円277円347円224円250円346円234円200円345円260円217円350円212円261円350円264円271円347円210円254円346円245円274円346円242円257円.md" @@ -6,7 +6,7 @@ # 746. 使用最小花费爬楼梯 -[力扣题目链接](https://leetcode-cn.com/problems/min-cost-climbing-stairs/) +[力扣题目链接](https://leetcode.cn/problems/min-cost-climbing-stairs/) 数组的每个下标作为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i](下标从 0 开始)。 diff --git "a/problems/0763.345円210円222円345円210円206円345円255円227円346円257円215円345円214円272円351円227円264円.md" "b/problems/0763.345円210円222円345円210円206円345円255円227円346円257円215円345円214円272円351円227円264円.md" index 2f4d1b4861..eb21e42f97 100644 --- "a/problems/0763.345円210円222円345円210円206円345円255円227円346円257円215円345円214円272円351円227円264円.md" +++ "b/problems/0763.345円210円222円345円210円206円345円255円227円346円257円215円345円214円272円351円227円264円.md" @@ -7,7 +7,7 @@ # 763.划分字母区间 -[力扣题目链接](https://leetcode-cn.com/problems/partition-labels/) +[力扣题目链接](https://leetcode.cn/problems/partition-labels/) 字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。 diff --git "a/problems/0841.351円222円245円345円214円231円345円222円214円346円210円277円351円227円264円.md" "b/problems/0841.351円222円245円345円214円231円345円222円214円346円210円277円351円227円264円.md" index 1cd130653b..6f51b4addb 100644 --- "a/problems/0841.351円222円245円345円214円231円345円222円214円346円210円277円351円227円264円.md" +++ "b/problems/0841.351円222円245円345円214円231円345円222円214円346円210円277円351円227円264円.md" @@ -8,7 +8,7 @@ # 841.钥匙和房间 -[力扣题目链接](https://leetcode-cn.com/problems/keys-and-rooms/) +[力扣题目链接](https://leetcode.cn/problems/keys-and-rooms/) 有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。 diff --git "a/problems/0844.346円257円224円350円276円203円345円220円253円351円200円200円346円240円274円347円232円204円345円255円227円347円254円246円344円270円262円.md" "b/problems/0844.346円257円224円350円276円203円345円220円253円351円200円200円346円240円274円347円232円204円345円255円227円347円254円246円344円270円262円.md" index dccc5404cc..c0773653d0 100644 --- "a/problems/0844.346円257円224円350円276円203円345円220円253円351円200円200円346円240円274円347円232円204円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0844.346円257円224円350円276円203円345円220円253円351円200円200円346円240円274円347円232円204円345円255円227円347円254円246円344円270円262円.md" @@ -7,7 +7,7 @@ # 844.比较含退格的字符串 -[力扣题目链接](https://leetcode-cn.com/problems/backspace-string-compare/) +[力扣题目链接](https://leetcode.cn/problems/backspace-string-compare/) 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。 diff --git "a/problems/0860.346円237円240円346円252円254円346円260円264円346円211円276円351円233円266円.md" "b/problems/0860.346円237円240円346円252円254円346円260円264円346円211円276円351円233円266円.md" index aa09e1c67d..f5785a912d 100644 --- "a/problems/0860.346円237円240円346円252円254円346円260円264円346円211円276円351円233円266円.md" +++ "b/problems/0860.346円237円240円346円252円254円346円260円264円346円211円276円351円233円266円.md" @@ -7,7 +7,7 @@ # 860.柠檬水找零 -[力扣题目链接](https://leetcode-cn.com/problems/lemonade-change/) +[力扣题目链接](https://leetcode.cn/problems/lemonade-change/) 在柠檬水摊上,每一杯柠檬水的售价为 5 美元。 diff --git "a/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" "b/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" index cb564fb683..8ca46db9b5 100644 --- "a/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" +++ "b/problems/0922.346円214円211円345円245円207円345円201円266円346円216円222円345円272円217円346円225円260円347円273円204円II.md" @@ -9,7 +9,7 @@ # 922. 按奇偶排序数组II -[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/) +[力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/) 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。 diff --git "a/problems/0925.351円225円277円346円214円211円351円224円256円345円205円245円.md" "b/problems/0925.351円225円277円346円214円211円351円224円256円345円205円245円.md" index 0ef5a3d7f3..bb6aeff257 100644 --- "a/problems/0925.351円225円277円346円214円211円351円224円256円345円205円245円.md" +++ "b/problems/0925.351円225円277円346円214円211円351円224円256円345円205円245円.md" @@ -6,7 +6,7 @@ # 925.长按键入 -[力扣题目链接](https://leetcode-cn.com/problems/long-pressed-name/) +[力扣题目链接](https://leetcode.cn/problems/long-pressed-name/) 你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。 diff --git "a/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" "b/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" index 4b7a978c0e..310dd35a1c 100644 --- "a/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" +++ "b/problems/0941.346円234円211円346円225円210円347円232円204円345円261円261円350円204円211円346円225円260円347円273円204円.md" @@ -7,7 +7,7 @@ # 941.有效的山脉数组 -[力扣题目链接](https://leetcode-cn.com/problems/valid-mountain-array/) +[力扣题目链接](https://leetcode.cn/problems/valid-mountain-array/) 给定一个整数数组 arr,如果它是有效的山脉数组就返回 true,否则返回 false。 @@ -157,6 +157,26 @@ var validMountainArray = function(arr) { }; ``` +## TypeScript + +```typescript +function validMountainArray(arr: number[]): boolean { + const length: number = arr.length; + if (length < 3) return false; + let left: number = 0, + right: number = length - 1; + while (left < (length - 1) && arr[left] < arr[left + 1]) { + left++; + } + while (right> 0 && arr[right] < arr[right - 1]) { + right--; + } + if (left === right && left !== 0 && right !== length - 1) + return true; + return false; +}; +``` + diff --git "a/problems/0968.347円233円221円346円216円247円344円272円214円345円217円211円346円240円221円.md" "b/problems/0968.347円233円221円346円216円247円344円272円214円345円217円211円346円240円221円.md" index 9a510a1b79..0aa04a0297 100644 --- "a/problems/0968.347円233円221円346円216円247円344円272円214円345円217円211円346円240円221円.md" +++ "b/problems/0968.347円233円221円346円216円247円344円272円214円345円217円211円346円240円221円.md" @@ -7,7 +7,7 @@ # 968.监控二叉树 -[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-cameras/) +[力扣题目链接](https://leetcode.cn/problems/binary-tree-cameras/) 给定一个二叉树,我们在树的节点上安装摄像头。 diff --git "a/problems/0977.346円234円211円345円272円217円346円225円260円347円273円204円347円232円204円345円271円263円346円226円271円.md" "b/problems/0977.346円234円211円345円272円217円346円225円260円347円273円204円347円232円204円345円271円263円346円226円271円.md" index 20bdf7b040..4052c57027 100644 --- "a/problems/0977.346円234円211円345円272円217円346円225円260円347円273円204円347円232円204円345円271円263円346円226円271円.md" +++ "b/problems/0977.346円234円211円345円272円217円346円225円260円347円273円204円347円232円204円345円271円263円346円226円271円.md" @@ -8,7 +8,7 @@ # 977.有序数组的平方 -[力扣题目链接](https://leetcode-cn.com/problems/squares-of-a-sorted-array/) +[力扣题目链接](https://leetcode.cn/problems/squares-of-a-sorted-array/) 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。 diff --git "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" index 075b5ef1a6..6f54c0986a 100644 --- "a/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" +++ "b/problems/1002.346円237円245円346円211円276円345円270円270円347円224円250円345円255円227円347円254円246円.md" @@ -8,7 +8,7 @@ # 1002. 查找常用字符 -[力扣题目链接](https://leetcode-cn.com/problems/find-common-characters/) +[力扣题目链接](https://leetcode.cn/problems/find-common-characters/) 给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案。 diff --git "a/problems/1005.K346円254円241円345円217円226円345円217円215円345円220円216円346円234円200円345円244円247円345円214円226円347円232円204円346円225円260円347円273円204円345円222円214円.md" "b/problems/1005.K346円254円241円345円217円226円345円217円215円345円220円216円346円234円200円345円244円247円345円214円226円347円232円204円346円225円260円347円273円204円345円222円214円.md" index 202534da8c..8e161594fc 100644 --- "a/problems/1005.K346円254円241円345円217円226円345円217円215円345円220円216円346円234円200円345円244円247円345円214円226円347円232円204円346円225円260円347円273円204円345円222円214円.md" +++ "b/problems/1005.K346円254円241円345円217円226円345円217円215円345円220円216円346円234円200円345円244円247円345円214円226円347円232円204円346円225円260円347円273円204円345円222円214円.md" @@ -7,7 +7,7 @@ # 1005.K次取反后最大化的数组和 -[力扣题目链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) +[力扣题目链接](https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/) 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。) diff --git "a/problems/1035.344円270円215円347円233円270円344円272円244円347円232円204円347円272円277円.md" "b/problems/1035.344円270円215円347円233円270円344円272円244円347円232円204円347円272円277円.md" index 4463c5f713..83ccb08c10 100644 --- "a/problems/1035.344円270円215円347円233円270円344円272円244円347円232円204円347円272円277円.md" +++ "b/problems/1035.344円270円215円347円233円270円344円272円244円347円232円204円347円272円277円.md" @@ -6,7 +6,7 @@ ## 1035.不相交的线 -[力扣题目链接](https://leetcode-cn.com/problems/uncrossed-lines/) +[力扣题目链接](https://leetcode.cn/problems/uncrossed-lines/) 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。 diff --git "a/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md" "b/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md" index a92a3911e1..9691c75bed 100644 --- "a/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md" +++ "b/problems/1047.345円210円240円351円231円244円345円255円227円347円254円246円344円270円262円344円270円255円347円232円204円346円211円200円346円234円211円347円233円270円351円202円273円351円207円215円345円244円215円351円241円271円.md" @@ -11,7 +11,7 @@ # 1047. 删除字符串中的所有相邻重复项 -[力扣题目链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) +[力扣题目链接](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/) 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。 @@ -374,6 +374,34 @@ func removeDuplicates(_ s: String) -> String { return String(stack) } ``` + + +PHP: +```php +class Solution { + function removeDuplicates($s) { + $stack = new SplStack(); + for($i=0;$iisEmpty() || $s[$i] != $stack->top()){ + $stack->push($s[$i]); + }else{ + $stack->pop(); + } + } + + $result = ""; + while(!$stack->isEmpty()){ + $result.= $stack->top(); + $stack->pop(); + } + + // 此时字符串需要反转一下 + return strrev($result); + } +} +``` + + Scala: ```scala object Solution { @@ -396,5 +424,6 @@ object Solution { } } ``` + -----------------------
diff --git "a/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md" "b/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md" index f3e7909cb1..f4766069ef 100644 --- "a/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md" +++ "b/problems/1049.346円234円200円345円220円216円344円270円200円345円235円227円347円237円263円345円244円264円347円232円204円351円207円215円351円207円217円II.md" @@ -7,7 +7,7 @@ ## 1049. 最后一块石头的重量 II -[力扣题目链接](https://leetcode-cn.com/problems/last-stone-weight-ii/) +[力扣题目链接](https://leetcode.cn/problems/last-stone-weight-ii/) 题目难度:中等 diff --git "a/problems/1143.346円234円200円351円225円277円345円205円254円345円205円261円345円255円220円345円272円217円345円210円227円.md" "b/problems/1143.346円234円200円351円225円277円345円205円254円345円205円261円345円255円220円345円272円217円345円210円227円.md" index d58330ec11..ad9825b818 100644 --- "a/problems/1143.346円234円200円351円225円277円345円205円254円345円205円261円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/1143.346円234円200円351円225円277円345円205円254円345円205円261円345円255円220円345円272円217円345円210円227円.md" @@ -6,7 +6,7 @@ ## 1143.最长公共子序列 -[力扣题目链接](https://leetcode-cn.com/problems/longest-common-subsequence/) +[力扣题目链接](https://leetcode.cn/problems/longest-common-subsequence/) 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。 diff --git "a/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" "b/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" index ba92552ab8..5298f5becb 100644 --- "a/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" +++ "b/problems/1207.347円213円254円344円270円200円346円227円240円344円272円214円347円232円204円345円207円272円347円216円260円346円254円241円346円225円260円.md" @@ -6,7 +6,7 @@ # 1207.独一无二的出现次数 -[力扣题目链接](https://leetcode-cn.com/problems/unique-number-of-occurrences/) +[力扣题目链接](https://leetcode.cn/problems/unique-number-of-occurrences/) 给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。 @@ -150,5 +150,39 @@ var uniqueOccurrences = function(arr) { }; ``` +TypeScript: + +> 借用数组: + +```typescript +function uniqueOccurrences(arr: number[]): boolean { + const countArr: number[] = new Array(2001).fill(0); + for (let i = 0, length = arr.length; i < length; i++) { + countArr[arr[i] + 1000]++; + } + const flagArr: boolean[] = new Array(1001).fill(false); + for (let count of countArr) { + if (count === 0) continue; + if (flagArr[count] === true) return false; + flagArr[count] = true; + } + return true; +}; +``` + +> 借用map、set + +```typescript +function uniqueOccurrences(arr: number[]): boolean { + const countMap: Map = new Map(); + arr.forEach(val => { + countMap.set(val, (countMap.get(val) || 0) + 1); + }) + return countMap.size === new Set(countMap.values()).size; +}; +``` + + + -----------------------
diff --git "a/problems/1221.345円210円206円345円211円262円345円271円263円350円241円241円345円255円227円347円254円246円344円270円262円.md" "b/problems/1221.345円210円206円345円211円262円345円271円263円350円241円241円345円255円227円347円254円246円344円270円262円.md" index 1a9b34a29e..08d4fee719 100644 --- "a/problems/1221.345円210円206円345円211円262円345円271円263円350円241円241円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/1221.345円210円206円345円211円262円345円271円263円350円241円241円345円255円227円347円254円246円344円270円262円.md" @@ -6,7 +6,7 @@ # 1221. 分割平衡字符串 -[力扣题目链接](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/) +[力扣题目链接](https://leetcode.cn/problems/split-a-string-in-balanced-strings/) 在一个 平衡字符串 中,'L' 和 'R' 字符的数量是相同的。 diff --git "a/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" "b/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" index 838c3a9600..5ca73607c0 100644 --- "a/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" +++ "b/problems/1356.346円240円271円346円215円256円346円225円260円345円255円227円344円272円214円350円277円233円345円210円266円344円270円2131円347円232円204円346円225円260円347円233円256円346円216円222円345円272円217円.md" @@ -8,9 +8,9 @@ # 1356. 根据数字二进制下 1 的数目排序 -[力扣题目链接](https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/) +[力扣题目链接](https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits/) -题目链接:https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/ +题目链接:https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits/ 给你一个整数数组 arr 。请你将数组中的元素按照其二进制表示中数字 1 的数目升序排序。 diff --git "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" index ce1e77dfa7..3aaadf6d8c 100644 --- "a/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" +++ "b/problems/1365.346円234円211円345円244円232円345円260円221円345円260円217円344円272円216円345円275円223円345円211円215円346円225円260円345円255円227円347円232円204円346円225円260円345円255円227円.md" @@ -8,7 +8,7 @@ # 1365.有多少小于当前数字的数字 -[力扣题目链接](https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/) +[力扣题目链接](https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number/) 给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。 diff --git "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" index 57231ec46b..d944ac3016 100644 --- "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" +++ "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" @@ -7,7 +7,7 @@ # 1382.将二叉搜索树变平衡 -[力扣题目链接](https://leetcode-cn.com/problems/balance-a-binary-search-tree/) +[力扣题目链接](https://leetcode.cn/problems/balance-a-binary-search-tree/) 给你一棵二叉搜索树,请你返回一棵 平衡后 的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。 diff --git "a/problems/344円272円214円345円217円211円346円240円221円347円232円204円350円277円255円344円273円243円351円201円215円345円216円206円.md" "b/problems/344円272円214円345円217円211円346円240円221円347円232円204円350円277円255円344円273円243円351円201円215円345円216円206円.md" index fac30f9989..dc8e812c1c 100644 --- "a/problems/344円272円214円345円217円211円346円240円221円347円232円204円350円277円255円344円273円243円351円201円215円345円216円206円.md" +++ "b/problems/344円272円214円345円217円211円346円240円221円347円232円204円350円277円255円344円273円243円351円201円215円345円216円206円.md" @@ -11,9 +11,9 @@ 看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目: -* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/) -* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) -* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) +* [144.二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/) +* [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) +* [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) 为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢? diff --git "a/problems/344円272円214円345円217円211円346円240円221円347円232円204円351円200円222円345円275円222円351円201円215円345円216円206円.md" "b/problems/344円272円214円345円217円211円346円240円221円347円232円204円351円200円222円345円275円222円351円201円215円345円216円206円.md" index 29c0cfdaa4..1cce2a0db9 100644 --- "a/problems/344円272円214円345円217円211円346円240円221円347円232円204円351円200円222円345円275円222円351円201円215円345円216円206円.md" +++ "b/problems/344円272円214円345円217円211円346円240円221円347円232円204円351円200円222円345円275円222円351円201円215円345円216円206円.md" @@ -99,9 +99,9 @@ void traversal(TreeNode* cur, vector& vec) { 此时大家可以做一做leetcode上三道题目,分别是: -* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/) -* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) -* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) +* [144.二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/) +* [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) +* [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) 可能有同学感觉前后中序遍历的递归太简单了,要打迭代法(非递归),别急,我们明天打迭代法,打个通透! diff --git "a/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md" "b/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md" index eecd7f0c56..78b03b17be 100644 --- "a/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md" +++ "b/problems/345円211円221円346円214円207円Offer05.346円233円277円346円215円242円347円251円272円346円240円274円.md" @@ -7,7 +7,7 @@ # 题目:剑指Offer 05.替换空格 -[力扣题目链接](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/) +[力扣题目链接](https://leetcode.cn/problems/ti-huan-kong-ge-lcof/) 请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 @@ -467,6 +467,42 @@ object Solution { def replaceSpace(s: String): String = { s.map(c => if(c == ' ') "%20" else c).mkString } + } +``` + + +PHP: +```php +function replaceSpace($s){ + $sLen = strlen($s); + $moreLen = $this->spaceLen($s) * 2; + + $head = $sLen - 1; + $tail = $sLen + $moreLen - 1; + + $s = $s . str_repeat(' ', $moreLen); + while ($head != $tail) { + if ($s[$head] == ' ') { + $s[$tail--] = '0'; + $s[$tail--] = '2'; + $s[$tail] = '%'; + } else { + $s[$tail] = $s[$head]; + } + $head--; + $tail--; + } + return $s; +} +// 统计空格个数 +function spaceLen($s){ + $count = 0; + for ($i = 0; $i < strlen($s); $i++) { + if ($s[$i] == ' ') { + $count++; + } + } + return $count; } ``` diff --git "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" index 7c39ed69ae..4674c14170 100644 --- "a/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/345円211円221円346円214円207円Offer58-II.345円267円246円346円227円213円350円275円254円345円255円227円347円254円246円344円270円262円.md" @@ -9,7 +9,7 @@ # 题目:剑指Offer58-II.左旋转字符串 -[力扣题目链接](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/) +[力扣题目链接](https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/) 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。 diff --git "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" index 0a38cc3309..1ae01061ad 100644 --- "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" +++ "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" @@ -7,7 +7,7 @@ # 面试题 02.07. 链表相交 -[力扣题目链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/) +[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/) 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

AltStyle によって変換されたページ (->オリジナル) /