Skip to content

Commit 71d7e62

Browse files
authored
Merge branch 'master' into master
2 parents e8851b0 + 6609258 commit 71d7e62

File tree

7 files changed

+230
-16
lines changed

7 files changed

+230
-16
lines changed

problems/0001.两数之和.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ function twoSum(array $nums, int $target): array
206206
}
207207
```
208208

209+
Swift:
210+
```swift
211+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
212+
var res = [Int]()
213+
var dict = [Int : Int]()
214+
for i in 0 ..< nums.count {
215+
let other = target - nums[i]
216+
if dict.keys.contains(other) {
217+
res.append(i)
218+
res.append(dict[other]!)
219+
return res
220+
}
221+
dict[nums[i]] = i
222+
}
223+
return res
224+
}
225+
```
209226

210227
-----------------------
211228
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0027.移除元素.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,21 @@ class Solution {
268268
}
269269
return $slow;
270270
}
271+
```
272+
273+
C:
274+
```c
275+
int removeElement(int* nums, int numsSize, int val){
276+
int slow = 0;
277+
for(int fast = 0; fast < numsSize; fast++) {
278+
//若快指针位置的元素不等于要删除的元素
279+
if(nums[fast] != val) {
280+
//将其挪到慢指针指向的位置,慢指针+1
281+
nums[slow++] = nums[fast];
282+
}
283+
}
284+
//最后慢指针的大小就是新的数组的大小
285+
return slow;
271286
}
272287
```
273288

problems/0077.组合.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,59 @@ func backtrack(n,k,start int,track []int){
435435
}
436436
```
437437

438+
C:
439+
```c
440+
int* path;
441+
int pathTop;
442+
int** ans;
443+
int ansTop;
444+
445+
void backtracking(int n, int k,int startIndex) {
446+
//当path中元素个数为k个时,我们需要将path数组放入ans二维数组中
447+
if(pathTop == k) {
448+
//path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化
449+
//因此创建新的数组存储path中的值
450+
int* temp = (int*)malloc(sizeof(int) * k);
451+
int i;
452+
for(i = 0; i < k; i++) {
453+
temp[i] = path[i];
454+
}
455+
ans[ansTop++] = temp;
456+
return ;
457+
}
458+
459+
int j;
460+
for(j = startIndex; j <=n ;j++) {
461+
//将当前结点放入path数组
462+
path[pathTop++] = j;
463+
//进行递归
464+
backtracking(n, k, j + 1);
465+
//进行回溯,将数组最上层结点弹出
466+
pathTop--;
467+
}
468+
}
469+
470+
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
471+
//path数组存储符合条件的结果
472+
path = (int*)malloc(sizeof(int) * k);
473+
//ans二维数组存储符合条件的结果数组的集合。(数组足够大,避免极端情况)
474+
ans = (int**)malloc(sizeof(int*) * 10000);
475+
pathTop = ansTop = 0;
476+
477+
//回溯算法
478+
backtracking(n, k, 1);
479+
//最后的返回大小为ans数组大小
480+
*returnSize = ansTop;
481+
//returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k)
482+
*returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize));
483+
int i;
484+
for(i = 0; i < *returnSize; i++) {
485+
(*returnColumnSizes)[i] = k;
486+
}
487+
//返回ans二维数组
488+
return ans;
489+
}
490+
```
438491
439492
-----------------------
440493
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0202.快乐数.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,37 @@ var isHappy = function(n) {
191191
};
192192
```
193193

194-
194+
Swift:
195+
```swift
196+
// number 每个位置上的数字的平方和
197+
func getSum(_ number: Int) -> Int {
198+
var sum = 0
199+
var num = number
200+
while num > 0 {
201+
let temp = num % 10
202+
sum += (temp * temp)
203+
num /= 10
204+
}
205+
return sum
206+
}
207+
func isHappy(_ n: Int) -> Bool {
208+
var set = Set<Int>()
209+
var num = n
210+
while true {
211+
let sum = self.getSum(num)
212+
if sum == 1 {
213+
return true
214+
}
215+
// 如果这个sum曾经出现过,说明已经陷入了无限循环了
216+
if set.contains(sum) {
217+
return false
218+
} else {
219+
set.insert(sum)
220+
}
221+
num = sum
222+
}
223+
}
224+
```
195225

196226
-----------------------
197227
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0225.用队列实现栈.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,71 @@ class MyStack:
359359

360360
Go:
361361

362+
```go
363+
type MyStack struct {
364+
queue []int//创建一个队列
365+
}
366+
367+
368+
/** Initialize your data structure here. */
369+
func Constructor() MyStack {
370+
return MyStack{ //初始化
371+
queue:make([]int,0),
372+
}
373+
}
374+
375+
376+
/** Push element x onto stack. */
377+
func (this *MyStack) Push(x int) {
378+
//添加元素
379+
this.queue=append(this.queue,x)
380+
}
381+
382+
383+
/** Removes the element on top of the stack and returns that element. */
384+
func (this *MyStack) Pop() int {
385+
n:=len(this.queue)-1//判断长度
386+
for n!=0{ //除了最后一个,其余的都重新添加到队列里
387+
val:=this.queue[0]
388+
this.queue=this.queue[1:]
389+
this.queue=append(this.queue,val)
390+
n--
391+
}
392+
//弹出元素
393+
val:=this.queue[0]
394+
this.queue=this.queue[1:]
395+
return val
396+
397+
}
398+
399+
400+
/** Get the top element. */
401+
func (this *MyStack) Top() int {
402+
//利用Pop函数,弹出来的元素重新添加
403+
val:=this.Pop()
404+
this.queue=append(this.queue,val)
405+
return val
406+
}
407+
408+
409+
/** Returns whether the stack is empty. */
410+
func (this *MyStack) Empty() bool {
411+
return len(this.queue)==0
412+
}
413+
414+
415+
/**
416+
* Your MyStack object will be instantiated and called as such:
417+
* obj := Constructor();
418+
* obj.Push(x);
419+
* param_2 := obj.Pop();
420+
* param_3 := obj.Top();
421+
* param_4 := obj.Empty();
422+
*/
423+
```
424+
425+
426+
362427
javaScript:
363428

364429
使用数组(push, shift)模拟队列

problems/0232.用栈实现队列.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,26 @@ class MyQueue:
205205

206206
def pop(self) -> int:
207207
"""
208-
1. 检查如果out里面元素,则直接pop
209-
2. 如果out没有元素,就把in里面的元素(除了第一个)依次pop后装进out里面
210-
3. 直接把in剩下的元素pop出来,就是queue头部的
208+
Removes the element from in front of queue and returns that element.
211209
"""
212-
if self.empty:
210+
if self.empty():
213211
return None
214212

215213
if self.stack_out:
216214
return self.stack_out.pop()
217215
else:
218-
for i in range(1, len(self.stack_in)):
216+
for i in range(len(self.stack_in)):
219217
self.stack_out.append(self.stack_in.pop())
220-
return self.stack_in.pop()
218+
return self.stack_out.pop()
221219

222220

223221
def peek(self) -> int:
224222
"""
225-
1. 查out有没有元素,有就把最上面的返回
226-
2. 如果out没有元素,就把in最下面的返回
223+
Get the front element.
227224
"""
228-
if self.empty:
229-
return None
230-
231-
if self.stack_out:
232-
return self.stack_out[-1]
233-
else:
234-
return self.stack_in[0]
225+
ans = self.pop()
226+
self.stack_out.append(ans)
227+
return ans
235228

236229

237230
def empty(self) -> bool:

problems/1002.查找常用字符.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,47 @@ func min(a,b int)int{
268268
return a
269269
}
270270
```
271+
272+
Swift:
273+
```swift
274+
func commonChars(_ words: [String]) -> [String] {
275+
var res = [String]()
276+
if words.count < 1 {
277+
return res
278+
}
279+
let aUnicodeScalarValue = "a".unicodeScalars.first!.value
280+
let lettersMaxCount = 26
281+
// 用于统计所有字符串每个字母出现的 最小 频率
282+
var hash = Array(repeating: 0, count: lettersMaxCount)
283+
// 统计第一个字符串每个字母出现的次数
284+
for unicodeScalar in words.first!.unicodeScalars {
285+
hash[Int(unicodeScalar.value - aUnicodeScalarValue)] += 1
286+
}
287+
// 统计除第一个字符串每个字母出现的次数
288+
for idx in 1 ..< words.count {
289+
var hashOtherStr = Array(repeating: 0, count: lettersMaxCount)
290+
for unicodeScalar in words[idx].unicodeScalars {
291+
hashOtherStr[Int(unicodeScalar.value - aUnicodeScalarValue)] += 1
292+
}
293+
// 更新hash,保证hash里统计的字母为出现的最小频率
294+
for k in 0 ..< lettersMaxCount {
295+
hash[k] = min(hash[k], hashOtherStr[k])
296+
}
297+
}
298+
// 将hash统计的字符次数,转成输出形式
299+
for i in 0 ..< lettersMaxCount {
300+
while hash[i] != 0 { // 注意这里是while,多个重复的字符
301+
let currentUnicodeScalarValue: UInt32 = UInt32(i) + aUnicodeScalarValue
302+
let currentUnicodeScalar: UnicodeScalar = UnicodeScalar(currentUnicodeScalarValue)!
303+
let outputStr = String(currentUnicodeScalar) // UnicodeScalar -> String
304+
res.append(outputStr)
305+
hash[i] -= 1
306+
}
307+
}
308+
return res
309+
}
310+
```
311+
271312
-----------------------
272313
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
273314
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

0 commit comments

Comments
 (0)