From dd11a104a48b4d63e8c479670fc38e26f70cb802 Mon Sep 17 00:00:00 2001 From: martisss <2466632626@qq.com> Date: Thu, 2 Sep 2021 11:27:27 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0530=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=B7=AE.md=20js=E9=80=92=E5=BD=92=E4=B8=8E=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 46f6b796..ae6719ec 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -265,7 +265,7 @@ func getMinimumDifference(root *TreeNode) int { ``` ## JavaScript - +递归 先转换为有序数组 ```javascript /** * Definition for a binary tree node. @@ -297,6 +297,47 @@ var getMinimumDifference = function (root) { return diff; }; ``` +递归 在递归的过程中更新最小值 +```js +var getMinimumDifference = function(root) { + let res = Infinity + let preNode = null + // 中序遍历 + const inorder = (node) => { + if(!node) return + inorder(node.left) + // 更新res + if(preNode) res = Math.min(res, node.val - preNode.val) + // 记录前一个节点 + preNode = node + inorder(node.right) + } + inorder(root) + return res +} +``` + +迭代 中序遍历 +```js +var getMinimumDifference = function(root) { + let stack = [] + let cur = root + let res = Infinity + let pre = null + while(cur || stack.length) { + if(cur) { + stack.push(cur) + cur = cur.left + } else { + cur = stack.pop() + if(pre) res = Math.min(res, cur.val - pre.val) + pre = cur + cur = cur.right + } + } + return res +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 6886111727e91ee5a616d9e1056a6be3447991a1 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 2 Sep 2021 12:17:45 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合优化.md | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 171023dd..136ceb34 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -242,8 +242,59 @@ var combine = function(n, k) { }; ``` +C: +```c +int* path; +int pathTop; +int** ans; +int ansTop; +void backtracking(int n, int k,int startIndex) { + //当path中元素个数为k个时,我们需要将path数组放入ans二维数组中 + if(pathTop == k) { + //path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化 + //因此创建新的数组存储path中的值 + int* temp = (int*)malloc(sizeof(int) * k); + int i; + for(i = 0; i < k; i++) { + temp[i] = path[i]; + } + ans[ansTop++] = temp; + return ; + } + int j; + for(j = startIndex; j <= n- (k - pathTop) + 1;j++) { + //将当前结点放入path数组 + path[pathTop++] = j; + //进行递归 + backtracking(n, k, j + 1); + //进行回溯,将数组最上层结点弹出 + pathTop--; + } +} + +int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ + //path数组存储符合条件的结果 + path = (int*)malloc(sizeof(int) * k); + //ans二维数组存储符合条件的结果数组的集合。(数组足够大,避免极端情况) + ans = (int**)malloc(sizeof(int*) * 10000); + pathTop = ansTop = 0; + + //回溯算法 + backtracking(n, k, 1); + //最后的返回大小为ans数组大小 + *returnSize = ansTop; + //returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k) + *returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize)); + int i; + for(i = 0; i < *returnSize; i++) { + (*returnColumnSizes)[i] = k; + } + //返回ans二维数组 + return ans; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 8a35955a04594b22d6ea07af06188580a5c6c9f4 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 2 Sep 2021 12:33:00 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md=20C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 13142853..089884fc 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -270,7 +270,38 @@ def sorted_squares(nums) end ``` +C: +```c +int* sortedSquares(int* nums, int numsSize, int* returnSize){ + //返回的数组大小就是原数组大小 + *returnSize = numsSize; + //创建两个指针,right指向数组最后一位元素,left指向数组第一位元素 + int right = numsSize - 1; + int left = 0; + //最后要返回的结果数组 + int* ans = (int*)malloc(sizeof(int) * numsSize); + int index; + for(index = numsSize - 1; index >= 0; index--) { + //左指针指向元素的平方 + int lSquare = nums[left] * nums[left]; + //右指针指向元素的平方 + int rSquare = nums[right] * nums[right]; + //若左指针指向元素平方比右指针指向元素平方大,将左指针指向元素平方放入结果数组。左指针右移一位 + if(lSquare > rSquare) { + ans[index] = lSquare; + left++; + } + //若右指针指向元素平方比左指针指向元素平方大,将右指针指向元素平方放入结果数组。右指针左移一位 + else { + ans[index] = rSquare; + right--; + } + } + //返回结果数组 + return ans; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From ac49e879e0c913a65e6a3955b066aafef1641bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Thu, 2 Sep 2021 13:29:07 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E7=AC=AC454?= =?UTF-8?q?=E9=A2=98.=E5=9B=9B=E6=95=B0=E7=9B=B8=E5=8A=A0II=20Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 4e61dc2f..1c3f45e7 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -220,8 +220,33 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { }; ``` - - +Swift: +```swift +func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int { + // key:a+b的数值,value:a+b数值出现的次数 + var map = [Int: Int]() + // 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中 + for i in 0 ..< nums1.count { + for j in 0 ..< nums2.count { + let sum1 = nums1[i] + nums2[j] + map[sum1] = (map[sum1] ?? 0) + 1 + } + } + // 统计a+b+c+d = 0 出现的次数 + var res = 0 + // 在遍历大num3和num4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 + for i in 0 ..< nums3.count { + for j in 0 ..< nums4.count { + let sum2 = nums3[i] + nums4[j] + let other = 0 - sum2 + if map.keys.contains(other) { + res += map[other]! + } + } + } + return res +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 186d4e4ca41d2de86e25a009bb89d454096e5405 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Thu, 2 Sep 2021 14:06:04 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF=20?= =?UTF-8?q?=E8=B7=9F=E8=8A=82=E7=82=B9=20->=20=E6=A0=B9=E8=8A=82=E7=82=B9?= =?UTF-8?q?=20=E4=B8=8D=E6=83=B3=20=20=20->=20=E4=B8=8D=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的迭代遍历.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index a1d65070..84363610 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -27,7 +27,7 @@ 我们先看一下前序遍历。 -前序遍历是中左右,每次先处理的是中间节点,那么先将跟节点放入栈中,然后将右孩子加入栈,再加入左孩子。 +前序遍历是中左右,每次先处理的是中间节点,那么先将根节点放入栈中,然后将右孩子加入栈,再加入左孩子。 为什么要先加入 右孩子,再加入左孩子呢? 因为这样出栈的时候才是中左右的顺序。 @@ -140,7 +140,7 @@ public: # 总结 -此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不想递归写法那样代码稍做调整,就可以实现前后中序。 +此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。 **这是因为前序遍历中访问节点(遍历节点)和处理节点(将元素放进result数组中)可以同步处理,但是中序就无法做到同步!** From f5a5cd882f3725cc56a95541bb738025e7b05d89 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Thu, 2 Sep 2021 17:22:28 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E4=BC=98=E5=8C=96=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20Python3=E8=A7=A3=E6=B3=95=20=E4=BB=BB=E4=BD=95=E5=9C=A8?= =?UTF-8?q?=20list=20=E5=A4=B4=E9=83=A8=E8=BF=9B=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E9=83=BD=E4=BC=9A=E6=8D=9F=E5=A4=B1=E4=B8=80?= =?UTF-8?q?=E5=AE=9A=E6=80=A7=E8=83=BD=20=E5=9C=A8=20Python=20=E4=B8=AD?= =?UTF-8?q?=E5=BA=94=E8=AF=A5=E4=BD=BF=E7=94=A8=20collections.deque=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E9=98=9F=E5=88=97=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=20=E5=9C=A8=E8=AF=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=B7=A6=E5=8F=B3=E4=B8=A4=E7=AB=AF=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E5=92=8C=E5=BC=B9=E5=87=BA=E5=85=83=E7=B4=A0=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=E9=83=BD=E6=8E=A5?= =?UTF-8?q?=E8=BF=91O(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 37 +++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index a57a92aa..07708e5a 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -87,26 +87,31 @@ public: python代码: -```python +```python3 class Solution: + """二叉树层序遍历迭代解法""" + def levelOrder(self, root: TreeNode) -> List[List[int]]: + results = [] if not root: - return [] + return results + + from collections import deque + que = deque([root]) + + while que: + size = len(que) + result = [] + for _ in range(size): + cur = que.popleft() + result.append(cur.val) + if cur.left: + que.append(cur.left) + if cur.right: + que.append(cur.right) + results.append(result) - queue = [root] - out_list = [] - - while queue: - length = len(queue) - in_list = [] - for _ in range(length): - curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个 - in_list.append(curnode.val) - if curnode.left: queue.append(curnode.left) - if curnode.right: queue.append(curnode.right) - out_list.append(in_list) - - return out_list + return results ``` java: From acd13fc7d81aae428ee7bfd735b9e06c593eee11 Mon Sep 17 00:00:00 2001 From: Wen Date: Thu, 2 Sep 2021 20:55:02 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E4=BC=98=E5=8C=96=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20=E4=B8=AD=E7=9A=84=20107.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86=20II=20Python3?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 40 +++++++++++------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 07708e5a..a02cf997 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -279,29 +279,29 @@ python代码: ```python class Solution: + """二叉树层序遍历II迭代解法""" + def levelOrderBottom(self, root: TreeNode) -> List[List[int]]: + results = [] if not root: - return [] - quene = [root] - out_list = [] + return results - while quene: - in_list = [] - for _ in range(len(quene)): - node = quene.pop(0) - in_list.append(node.val) - if node.left: - quene.append(node.left) - if node.right: - quene.append(node.right) - - out_list.append(in_list) - - out_list.reverse() - return out_list - -# 执行用时:36 ms, 在所有 Python3 提交中击败了92.00%的用户 -# 内存消耗:15.2 MB, 在所有 Python3 提交中击败了63.76%的用户 + from collections import deque + que = deque([root]) + + while que: + result = [] + for _ in range(len(que)): + cur = que.popleft() + result.append(cur.val) + if cur.left: + que.append(cur.left) + if cur.right: + que.append(cur.right) + results.append(result) + + results.reverse() + return results ``` Java: From fde18e8fddd5e2686ab46157344b5b3bbdb3d715 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Fri, 3 Sep 2021 08:36:36 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BC=98=E5=8C=96=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20=E4=B8=AD=E7=9A=84=20637.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=B9=B3=E5=9D=87=E5=80=BC=20Python3?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 41 +++++++++++------------ 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 07708e5a..4a0f62e0 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -633,32 +633,29 @@ python代码: ```python class Solution: + """二叉树层平均值迭代解法""" + def averageOfLevels(self, root: TreeNode) -> List[float]: + results = [] if not root: - return [] + return results - quene = deque([root]) - out_list = [] - - while quene: - in_list = [] - - for _ in range(len(quene)): - node = quene.popleft() - in_list.append(node.val) - if node.left: - quene.append(node.left) - if node.right: - quene.append(node.right) - - out_list.append(in_list) - - out_list = map(lambda x: sum(x) / len(x), out_list) - - return out_list + from collections import deque + que = deque([root]) -# 执行用时:56 ms, 在所有 Python3 提交中击败了81.48%的用户 -# 内存消耗:17 MB, 在所有 Python3 提交中击败了89.68%的用户 + while que: + size = len(que) + sum_ = 0 + for _ in range(size): + cur = que.popleft() + sum_ += cur.val + if cur.left: + que.append(cur.left) + if cur.right: + que.append(cur.right) + results.append(sum_ / size) + + return results ``` java: From 578e9908c5470016110e8668815871c9826ff248 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Fri, 3 Sep 2021 09:24:10 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E4=BC=98=E5=8C=96=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20=E4=B8=AD=E7=9A=84=20429.N=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20Python3=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 58 +++++++---------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 4a0f62e0..c25dbda4 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -825,52 +825,28 @@ public: python代码: ```python - class Solution: + """N叉树的层序遍历迭代法""" + def levelOrder(self, root: 'Node') -> List[List[int]]: + results = [] if not root: - return [] + return results - quene = deque([root]) - out_list = [] - - while quene: - in_list = [] - - for _ in range(len(quene)): - node = quene.popleft() - in_list.append(node.val) - if node.children: - # 这个地方要用extend而不是append,我们看下面的例子: - # In [18]: alist=[] - # In [19]: alist.append([1,2,3]) - # In [20]: alist - # Out[20]: [[1, 2, 3]] - # In [21]: alist.extend([4,5,6]) - # In [22]: alist - # Out[22]: [[1, 2, 3], 4, 5, 6] - # 可以看到extend对要添加的list进行了一个解包操作 - # print(root.children),可以得到children是一个包含 - # 孩子节点地址的list,我们使用for遍历quene的时候, - # 希望quene是一个单层list,所以要用extend - # 使用extend的情况,如果print(quene),结果是 - # deque([<__main__.Node object at 0x7f60763ae0a0>]) - # deque([<__main__.Node object at 0x7f607636e6d0>, <__main__.Node object at 0x7f607636e130>, <__main__.Node object at 0x7f607636e310>]) - # deque([<__main__.Node object at 0x7f607636e880>, <__main__.Node object at 0x7f607636ef10>]) - # 可以看到是单层list - # 如果使用append,print(quene)的结果是 - # deque([<__main__.Node object at 0x7f18907530a0>]) - # deque([[<__main__.Node object at 0x7f18907136d0>, <__main__.Node object at 0x7f1890713130>, <__main__.Node object at 0x7f1890713310>]]) - # 可以看到是两层list,这样for的遍历就会报错 - - quene.extend(node.children) - - out_list.append(in_list) + from collections import deque + que = deque([root]) - return out_list - -# 执行用时:60 ms, 在所有 Python3 提交中击败了76.99%的用户 -# 内存消耗:16.5 MB, 在所有 Python3 提交中击败了89.19%的用户 + while que: + result = [] + for _ in range(len(que)): + cur = que.popleft() + result.append(cur.val) + # cur.children 是 Node 对象组成的列表,也可能为 None + if cur.children: + que.extend(cur.children) + results.append(result) + + return results ``` java: From ed1b1d2d39b080e7e45c2a92cb11e631f7ec5d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Fri, 3 Sep 2021 13:25:14 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20383.=20=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 64503cef..18b1de9b 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -266,6 +266,28 @@ var canConstruct = function(ransomNote, magazine) { }; ``` +Swift: +```swift +func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { + var record = Array(repeating: 0, count: 26); + let aUnicodeScalarValue = "a".unicodeScalars.first!.value + for unicodeScalar in magazine.unicodeScalars { + // 通过record 记录 magazine 里各个字符出现的次数 + let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue) + record[idx] += 1 + } + for unicodeScalar in ransomNote.unicodeScalars { + // 遍历 ransomNote,在record里对应的字符个数做 -- 操作 + let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue) + record[idx] -= 1 + // 如果小于零说明在magazine没有 + if record[idx] < 0 { + return false + } + } + return true +} +``` ----------------------- From 767168a8b0ec3f9cc2b31a76355e98d89d87f3ea Mon Sep 17 00:00:00 2001 From: martisss <2466632626@qq.com> Date: Sat, 4 Sep 2021 10:58:23 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D355.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2.md=20js=E4=BB=A3=E7=A0=81=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 1e1c4afe..8e20c402 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -197,11 +197,10 @@ func findContentChildren(g []int, s []int) int { return child } - +``` Javascript: -​```Javascript - +``` var findContentChildren = function(g, s) { g = g.sort((a, b) => a - b) s = s.sort((a, b) => a - b)