From 3e34ff114a3716d1548065348b5d3bc9d5f0199e Mon Sep 17 00:00:00 2001 From: ArthurP Date: Tue, 31 Aug 2021 12:22:13 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=20C=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/0077.组合.md | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 01d1a537..9b44b572 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -435,6 +435,59 @@ func backtrack(n,k,start int,track []int){ } ``` +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 ;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 14aee4922bc43138b18f10ce52eb521706749b7b Mon Sep 17 00:00:00 2001 From: ArthurP Date: Tue, 31 Aug 2021 12:32:20 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.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/0027.移除元素.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e3b75719..886ce4f2 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -246,6 +246,23 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { } ``` +C: +```c +int removeElement(int* nums, int numsSize, int val){ + int slow = 0; + for(int fast = 0; fast < numsSize; fast++) { + //若快指针位置的元素不等于要删除的元素 + if(nums[fast] != val) { + //将其挪到慢指针指向的位置,慢指针+1 + nums[slow++] = nums[fast]; + } + } + //最后慢指针的大小就是新的数组的大小 + return slow; +} + +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From d65afddc9189f1644237aca61cfb77f69dcb8292 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Tue, 31 Aug 2021 12:34:01 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20C=E8=AF=AD=E8=A8=80=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 886ce4f2..ff50d511 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -260,7 +260,6 @@ int removeElement(int* nums, int numsSize, int val){ //最后慢指针的大小就是新的数组的大小 return slow; } - ``` ----------------------- From 29876fbdee24c4cd3e10faefca2fd1f8a70664ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Tue, 31 Aug 2021 13:02:33 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201002.=20=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6=20Swift=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/1002.查找常用字符.md | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index f7d323aa..c0ca578e 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -268,6 +268,47 @@ func min(a,b int)int{ return a } ``` + +Swift: +```swift +func commonChars(_ words: [String]) -> [String] { + var res = [String]() + if words.count < 1 { + return res + } + let aUnicodeScalarValue = "a".unicodeScalars.first!.value + let lettersMaxCount = 26 + // 用于统计所有字符串每个字母出现的 最小 频率 + var hash = Array(repeating: 0, count: lettersMaxCount) + // 统计第一个字符串每个字母出现的次数 + for unicodeScalar in words.first!.unicodeScalars { + hash[Int(unicodeScalar.value - aUnicodeScalarValue)] += 1 + } + // 统计除第一个字符串每个字母出现的次数 + for idx in 1 ..< words.count { + var hashOtherStr = Array(repeating: 0, count: lettersMaxCount) + for unicodeScalar in words[idx].unicodeScalars { + hashOtherStr[Int(unicodeScalar.value - aUnicodeScalarValue)] += 1 + } + // 更新hash,保证hash里统计的字母为出现的最小频率 + for k in 0 ..< lettersMaxCount { + hash[k] = min(hash[k], hashOtherStr[k]) + } + } + // 将hash统计的字符次数,转成输出形式 + for i in 0 ..< lettersMaxCount { + while hash[i] != 0 { // 注意这里是while,多个重复的字符 + let currentUnicodeScalarValue: UInt32 = UInt32(i) + aUnicodeScalarValue + let currentUnicodeScalar: UnicodeScalar = UnicodeScalar(currentUnicodeScalarValue)! + let outputStr = String(currentUnicodeScalar) // UnicodeScalar -> String + res.append(outputStr) + hash[i] -= 1 + } + } + return res +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 55753bb5580e9a25a6af1e9cc10834156bf149b7 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+GHumorBS@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:30:39 +0800 Subject: [PATCH 5/8] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 更新: 1. 更新前版本的self.pop()语法错误 2. 更新后C++范例逻辑趋于统一 --- problems/0232.用栈实现队列.md | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index a4a73603..9f6bb90f 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -205,33 +205,26 @@ class MyQueue: def pop(self) -> int: """ - 1. 检查如果out里面元素,则直接pop - 2. 如果out没有元素,就把in里面的元素(除了第一个)依次pop后装进out里面 - 3. 直接把in剩下的元素pop出来,就是queue头部的 + Removes the element from in front of queue and returns that element. """ - if self.empty: + if self.empty(): return None if self.stack_out: return self.stack_out.pop() else: - for i in range(1, len(self.stack_in)): + for i in range(len(self.stack_in)): self.stack_out.append(self.stack_in.pop()) - return self.stack_in.pop() + return self.stack_out.pop() def peek(self) -> int: """ - 1. 查out有没有元素,有就把最上面的返回 - 2. 如果out没有元素,就把in最下面的返回 + Get the front element. """ - if self.empty: - return None - - if self.stack_out: - return self.stack_out[-1] - else: - return self.stack_in[0] + ans = self.pop() + self.stack_out.append(ans) + return ans def empty(self) -> bool: From f172c95e882e4eb8cc13636469588f81c5695bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Tue, 31 Aug 2021 14:36:45 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E7=AC=AC202=E9=A2=98.?= =?UTF-8?q?=20=E5=BF=AB=E4=B9=90=E6=95=B0=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index b9386a68..e6365c71 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -191,7 +191,37 @@ var isHappy = function(n) { }; ``` - +Swift: +```swift +// number 每个位置上的数字的平方和 +func getSum(_ number: Int) -> Int { + var sum = 0 + var num = number + while num > 0 { + let temp = num % 10 + sum += (temp * temp) + num /= 10 + } + return sum +} +func isHappy(_ n: Int) -> Bool { + var set = Set() + var num = n + while true { + let sum = self.getSum(num) + if sum == 1 { + return true + } + // 如果这个sum曾经出现过,说明已经陷入了无限循环了 + if set.contains(sum) { + return false + } else { + set.insert(sum) + } + num = sum + } +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 71a24c5dd88a48d518a3be11ab9b68785c5dc66a Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Tue, 31 Aug 2021 15:57:51 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0225.=20=E7=94=A8=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index afa563e3..8d4db953 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -359,6 +359,71 @@ class MyStack: Go: +```go +type MyStack struct { + queue []int//创建一个队列 +} + + +/** Initialize your data structure here. */ +func Constructor() MyStack { + return MyStack{ //初始化 + queue:make([]int,0), + } +} + + +/** Push element x onto stack. */ +func (this *MyStack) Push(x int) { + //添加元素 + this.queue=append(this.queue,x) +} + + +/** Removes the element on top of the stack and returns that element. */ +func (this *MyStack) Pop() int { + n:=len(this.queue)-1//判断长度 + for n!=0{ //除了最后一个,其余的都重新添加到队列里 + val:=this.queue[0] + this.queue=this.queue[1:] + this.queue=append(this.queue,val) + n-- + } + //弹出元素 + val:=this.queue[0] + this.queue=this.queue[1:] + return val + +} + + +/** Get the top element. */ +func (this *MyStack) Top() int { + //利用Pop函数,弹出来的元素重新添加 + val:=this.Pop() + this.queue=append(this.queue,val) + return val +} + + +/** Returns whether the stack is empty. */ +func (this *MyStack) Empty() bool { + return len(this.queue)==0 +} + + +/** + * Your MyStack object will be instantiated and called as such: + * obj := Constructor(); + * obj.Push(x); + * param_2 := obj.Pop(); + * param_3 := obj.Top(); + * param_4 := obj.Empty(); + */ +``` + + + javaScript: 使用数组(push, shift)模拟队列 From 9d1983fd02c428babb8809706744caa06bf4e57b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Wed, 1 Sep 2021 14:45:25 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201.=20=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index fd17af62..f12b5869 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -206,6 +206,23 @@ function twoSum(array $nums, int $target): array } ``` +Swift: +```swift +func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + var res = [Int]() + var dict = [Int : Int]() + for i in 0 ..< nums.count { + let other = target - nums[i] + if dict.keys.contains(other) { + res.append(i) + res.append(dict[other]!) + return res + } + dict[nums[i]] = i + } + return res +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)