mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge branch 'master' into master
This commit is contained in:
@ -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)
|
||||
|
@ -268,6 +268,21 @@ class Solution {
|
||||
}
|
||||
return $slow;
|
||||
}
|
||||
```
|
||||
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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<Int>()
|
||||
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)
|
||||
|
@ -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)模拟队列
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user