diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md
index 634d8535..d67e5f70 100644
--- a/problems/0028.实现strStr.md
+++ b/problems/0028.实现strStr.md
@@ -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
+
+ }
+ }
+
+```
+
-----------------------
diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md
index 7910fc50..6401824b 100644
--- a/problems/0093.复原IP地址.md
+++ b/problems/0093.复原IP地址.md
@@ -227,7 +227,7 @@ private:
public:
vector 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;
}
diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md
index 7ecca773..188ad3cb 100644
--- a/problems/0106.从中序与后序遍历序列构造二叉树.md
+++ b/problems/0106.从中序与后序遍历序列构造二叉树.md
@@ -103,7 +103,7 @@ TreeNode* traversal (vector& inorder, vector& postorder) {
中序数组相对比较好切,找到切割点(后序数组的最后一个元素)在中序数组的位置,然后切割,如下代码中我坚持左闭右开的原则:
-```C++
+```CPP
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
@@ -130,7 +130,7 @@ vector rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
代码如下:
-```
+```CPP
// postorder 舍弃末尾元素,因为这个元素就是中间节点,已经用过了
postorder.resize(postorder.size() - 1);
@@ -144,7 +144,7 @@ vector rightPostorder(postorder.begin() + leftInorder.size(), postorder.end
接下来可以递归了,代码如下:
-```
+```CPP
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
```
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md
index 82a11381..fd72cf1b 100644
--- a/problems/0209.长度最小的子数组.md
+++ b/problems/0209.长度最小的子数组.md
@@ -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 {
+ pub fn min_sub_array_len(target: i32, nums: Vec) -> 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
+ }
+}
+```
+
-----------------------
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md
index f7ef8cf7..c71b2a93 100644
--- a/problems/0332.重新安排行程.md
+++ b/problems/0332.重新安排行程.md
@@ -342,7 +342,7 @@ class Solution:
return path
```
-### Go
+### GO
```go
type pair struct {
target string
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index 00707347..5d9e8295 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.md
@@ -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'] ++;
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md
index 33bbad55..2ab14b61 100644
--- a/problems/0452.用最少数量的箭引爆气球.md
+++ b/problems/0452.用最少数量的箭引爆气球.md
@@ -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)
{
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md
index ccfb485c..a51c68ee 100644
--- a/problems/0459.重复的子字符串.md
+++ b/problems/0459.重复的子字符串.md
@@ -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
+ }
+ }
+```
+
-----------------------
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index 35d19d7b..612f2394 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -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;
}
diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md
index faa1dc46..3ec399f1 100644
--- a/problems/背包问题理论基础完全背包.md
+++ b/problems/背包问题理论基础完全背包.md
@@ -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]);