Merge branch 'youngyangyang04:master' into master

This commit is contained in:
梦华
2022-05-10 21:28:50 +08:00
committed by GitHub
10 changed files with 256 additions and 16 deletions

View File

@ -1059,5 +1059,112 @@ func getNext(_ next: inout [Int], needle: [Character]) {
```
> 前缀表右移
```swift
func strStr(_ haystack: String, _ needle: String) -> Int {
let s = Array(haystack), p = Array(needle)
guard p.count != 0 else { return 0 }
var j = 0
var next = [Int].init(repeating: 0, count: p.count)
getNext(&next, p)
for i in 0 ..< s.count {
while j > 0 && s[i] != p[j] {
j = next[j]
}
if s[i] == p[j] {
j += 1
}
if j == p.count {
return i - p.count + 1
}
}
return -1
}
// 前缀表后移一位,首位用 -1 填充
func getNext(_ next: inout [Int], _ needle: [Character]) {
guard needle.count > 1 else { return }
var j = 0
next[0] = j
for i in 1 ..< needle.count-1 {
while j > 0 && needle[i] != needle[j] {
j = next[j-1]
}
if needle[i] == needle[j] {
j += 1
}
next[i] = j
}
next.removeLast()
next.insert(-1, at: 0)
}
```
> 前缀表统一不减一
```swift
func strStr(_ haystack: String, _ needle: String) -> Int {
let s = Array(haystack), p = Array(needle)
guard p.count != 0 else { return 0 }
var j = 0
var next = [Int](repeating: 0, count: needle.count)
// KMP
getNext(&next, needle: p)
for i in 0 ..< s.count {
while j > 0 && s[i] != p[j] {
j = next[j-1]
}
if s[i] == p[j] {
j += 1
}
if j == p.count {
return i - p.count + 1
}
}
return -1
}
//前缀表
func getNext(_ next: inout [Int], needle: [Character]) {
var j = 0
next[0] = j
for i in 1 ..< needle.count {
while j>0 && needle[i] != needle[j] {
j = next[j-1]
}
if needle[i] == needle[j] {
j += 1
}
next[i] = j
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -227,7 +227,7 @@ private:
public:
vector<string> restoreIpAddresses(string s) {
result.clear();
if (s.size() > 12) return result; // 算是剪枝了
if (s.size() < 4 || s.size() > 12) return result; // 算是剪枝了
backtracking(s, 0, 0);
return result;
}

View File

@ -103,7 +103,7 @@ TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
中序数组相对比较好切,找到切割点(后序数组的最后一个元素)在中序数组的位置,然后切割,如下代码中我坚持左闭右开的原则:
```C++
```CPP
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
@ -130,7 +130,7 @@ vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
代码如下:
```
```CPP
// postorder 舍弃末尾元素,因为这个元素就是中间节点,已经用过了
postorder.resize(postorder.size() - 1);
@ -144,7 +144,7 @@ vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end
接下来可以递归了,代码如下:
```
```CPP
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
```

View File

@ -198,7 +198,7 @@ JavaScript:
var minSubArrayLen = function(target, nums) {
// 长度计算一次
const len = nums.length;
let l = r = sum = 0,
let l = r = sum = 0,
res = len + 1; // 子数组最大不会超过自身
while(r < len) {
sum += nums[r++];
@ -260,12 +260,12 @@ Rust:
```rust
impl Solution {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0);
let (mut sum, mut i) = (0, 0);
for (pos, val) in nums.iter().enumerate() {
sum += val;
sum += val;
while sum >= target {
subLength = (pos - i + 1) as i32;
if result > subLength {
@ -364,7 +364,7 @@ int minSubArrayLen(int target, int* nums, int numsSize){
int minLength = INT_MAX;
int sum = 0;
int left = 0, right = 0;
int left = 0, right = 0;
//右边界向右扩展
for(; right < numsSize; ++right) {
sum += nums[right];
@ -380,5 +380,26 @@ int minSubArrayLen(int target, int* nums, int numsSize){
}
```
Kotlin:
```kotlin
class Solution {
fun minSubArrayLen(target: Int, nums: IntArray): Int {
var start = 0
var end = 0
var ret = Int.MAX_VALUE
var count = 0
while (end < nums.size) {
count += nums[end]
while (count >= target) {
ret = if (ret > (end - start + 1)) end - start + 1 else ret
count -= nums[start++]
}
end++
}
return if (ret == Int.MAX_VALUE) 0 else ret
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -342,7 +342,7 @@ class Solution:
return path
```
### Go
### GO
```go
type pair struct {
target string

View File

@ -84,6 +84,10 @@ class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int record[26] = {0};
//add
if (ransomNote.size() > magazine.size()) {
return false;
}
for (int i = 0; i < magazine.length(); i++) {
// 通过recode数据记录 magazine里各个字符出现次数
record[magazine[i]-'a'] ++;

View File

@ -193,7 +193,7 @@ func min(a,b int) int{
}
return a
}
```
```
### Javascript
```Javascript
@ -214,7 +214,31 @@ var findMinArrowShots = function(points) {
};
```
### TypeScript
```typescript
function findMinArrowShots(points: number[][]): number {
const length: number = points.length;
if (length === 0) return 0;
points.sort((a, b) => a[0] - b[0]);
let resCount: number = 1;
let right: number = points[0][1]; // 右边界
let tempPoint: number[];
for (let i = 1; i < length; i++) {
tempPoint = points[i];
if (tempPoint[0] > right) {
resCount++;
right = tempPoint[1];
} else {
right = Math.min(right, tempPoint[1]);
}
}
return resCount;
};
```
### C
```c
int cmp(const void *a,const void *b)
{

View File

@ -421,5 +421,89 @@ function repeatedSubstringPattern(s: string): boolean {
};
```
Swift:
> 前缀表统一减一
```swift
func repeatedSubstringPattern(_ s: String) -> Bool {
let sArr = Array(s)
let len = s.count
if len == 0 {
return false
}
var next = Array.init(repeating: -1, count: len)
getNext(&next,sArr)
if next.last != -1 && len % (len - (next[len-1] + 1)) == 0{
return true
}
return false
}
func getNext(_ next: inout [Int], _ str:[Character]) {
var j = -1
next[0] = j
for i in 1 ..< str.count {
while j >= 0 && str[j+1] != str[i] {
j = next[j]
}
if str[i] == str[j+1] {
j += 1
}
next[i] = j
}
}
```
> 前缀表统一不减一
```swift
func repeatedSubstringPattern(_ s: String) -> Bool {
let sArr = Array(s)
let len = sArr.count
if len == 0 {
return false
}
var next = Array.init(repeating: 0, count: len)
getNext(&next, sArr)
if next[len-1] != 0 && len % (len - next[len-1]) == 0 {
return true
}
return false
}
// 前缀表不减一
func getNext(_ next: inout [Int], _ sArr:[Character]) {
var j = 0
next[0] = 0
for i in 1 ..< sArr.count {
while j > 0 && sArr[i] != sArr[j] {
j = next[j-1]
}
if sArr[i] == sArr[j] {
j += 1
}
next[i] = j
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -371,18 +371,18 @@ C:
```c
//前序遍历:
void preOrderTraversal(struct TreeNode* root, int* ret, int* returnSize) {
void preOrder(struct TreeNode* root, int* ret, int* returnSize) {
if(root == NULL)
return;
ret[(*returnSize)++] = root->val;
preOrderTraverse(root->left, ret, returnSize);
preOrderTraverse(root->right, ret, returnSize);
preOrder(root->left, ret, returnSize);
preOrder(root->right, ret, returnSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int* ret = (int*)malloc(sizeof(int) * 100);
*returnSize = 0;
preOrderTraversal(root, ret, returnSize);
preOrder(root, ret, returnSize);
return ret;
}

View File

@ -37,7 +37,7 @@
* [动态规划关于01背包问题你该了解这些滚动数组](https://programmercarl.com/背包理论基础01背包-2.html)
首先在回顾一下01背包的核心代码
```
```cpp
for(int i = 0; i < weight.size(); i++) { // 遍历物品
for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);