Skip to content

Commit cc78fdd

Browse files
committed
Add solution 0097、0523、0525、1465、1744
1 parent c0191c2 commit cc78fdd

File tree

42 files changed

+1447
-173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1447
-173
lines changed

README.md

Lines changed: 128 additions & 128 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
func isInterleave(s1 string, s2 string, s3 string) bool {
4+
if len(s1)+len(s2) != len(s3) {
5+
return false
6+
}
7+
visited := make(map[int]bool)
8+
return dfs(s1, s2, s3, 0, 0, visited)
9+
}
10+
11+
func dfs(s1, s2, s3 string, p1, p2 int, visited map[int]bool) bool {
12+
if p1+p2 == len(s3) {
13+
return true
14+
}
15+
if _, ok := visited[(p1*len(s3))+p2]; ok {
16+
return false
17+
}
18+
visited[(p1*len(s3))+p2] = true
19+
var match1, match2 bool
20+
if p1 < len(s1) && s3[p1+p2] == s1[p1] {
21+
match1 = true
22+
}
23+
if p2 < len(s2) && s3[p1+p2] == s2[p2] {
24+
match2 = true
25+
}
26+
if match1 && match2 {
27+
return dfs(s1, s2, s3, p1+1, p2, visited) || dfs(s1, s2, s3, p1, p2+1, visited)
28+
} else if match1 {
29+
return dfs(s1, s2, s3, p1+1, p2, visited)
30+
} else if match2 {
31+
return dfs(s1, s2, s3, p1, p2+1, visited)
32+
} else {
33+
return false
34+
}
35+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question97 struct {
9+
para97
10+
ans97
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para97 struct {
16+
s1 string
17+
s2 string
18+
s3 string
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans97 struct {
24+
one bool
25+
}
26+
27+
func Test_Problem97(t *testing.T) {
28+
29+
qs := []question97{
30+
31+
{
32+
para97{"aabcc", "dbbca", "aadbbcbcac"},
33+
ans97{true},
34+
},
35+
36+
{
37+
para97{"aabcc", "dbbca", "aadbbbaccc"},
38+
ans97{false},
39+
},
40+
41+
{
42+
para97{"", "", ""},
43+
ans97{true},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 97------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans97, q.para97
51+
fmt.Printf("【input】:%v 【output】:%v\n", p, isInterleave(p.s1, p.s2, p.s3))
52+
}
53+
fmt.Printf("\n\n\n")
54+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [97. Interleaving String](https://leetcode.com/problems/interleaving-string/)
2+
3+
4+
## 题目
5+
6+
Given strings `s1``s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
7+
8+
An **interleaving** of two strings `s` and `t` is a configuration where they are divided into **non-empty** substrings such that:
9+
10+
- `s = s1 + s2 + ... + sn`
11+
- `t = t1 + t2 + ... + tm`
12+
- `|n - m| <= 1`
13+
- The **interleaving** is `s1 + t1 + s2 + t2 + s3 + t3 + ...` or `t1 + s1 + t2 + s2 + t3 + s3 + ...`
14+
15+
**Note:** `a + b` is the concatenation of strings `a` and `b`.
16+
17+
**Example 1:**
18+
19+
![https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg](https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg)
20+
21+
```
22+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
23+
Output: true
24+
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
31+
Output: false
32+
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: s1 = "", s2 = "", s3 = ""
39+
Output: true
40+
41+
```
42+
43+
**Constraints:**
44+
45+
- `0 <= s1.length, s2.length <= 100`
46+
- `0 <= s3.length <= 200`
47+
- `s1``s2`, and `s3` consist of lowercase English letters.
48+
49+
**Follow up:** Could you solve it using only `O(s2.length)` additional memory space?
50+
51+
## 题目大意
52+
53+
给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
54+
55+
- s = s1 + s2 + ... + sn
56+
- t = t1 + t2 + ... + tm
57+
- |n - m| <= 1
58+
- 交错 是 s1 + t1 + s2 + t2 + s3 + t3 + ... 或者 t1 + s1 + t2 + s2 + t3 + s3 + ...
59+
60+
提示:a + b 意味着字符串 a 和 b 连接。
61+
62+
## 解题思路
63+
64+
- 深搜或者广搜暴力解题。笔者用深搜实现的。记录 s1 和 s2 串当前比较的位置 p1 和 p2。如果 s3[p1+p2] 的位置上等于 s1[p1] 或者 s2[p2] 代表能匹配上,那么继续往后移动 p1 和 p2 相应的位置。因为是交错字符串,所以判断匹配的位置是 s3[p1+p2] 的位置。如果仅仅这么写,会超时,s1 和 s2 两个字符串重复交叉判断的位置太多了。需要加上记忆化搜索。可以用 visited[i][j] 这样的二维数组来记录是否搜索过了。笔者为了压缩空间,将 i 和 j 编码压缩到一维数组了。i * len(s3) + j 是唯一下标,所以可以用这种方式存储是否搜索过。具体代码见下面的实现。
65+
66+
## 代码
67+
68+
```go
69+
package leetcode
70+
71+
func isInterleave(s1 string, s2 string, s3 string) bool {
72+
if len(s1)+len(s2) != len(s3) {
73+
return false
74+
}
75+
visited := make(map[int]bool)
76+
return dfs(s1, s2, s3, 0, 0, visited)
77+
}
78+
79+
func dfs(s1, s2, s3 string, p1, p2 int, visited map[int]bool) bool {
80+
if p1+p2 == len(s3) {
81+
return true
82+
}
83+
if _, ok := visited[(p1*len(s3))+p2]; ok {
84+
return false
85+
}
86+
visited[(p1*len(s3))+p2] = true
87+
var match1, match2 bool
88+
if p1 < len(s1) && s3[p1+p2] == s1[p1] {
89+
match1 = true
90+
}
91+
if p2 < len(s2) && s3[p1+p2] == s2[p2] {
92+
match2 = true
93+
}
94+
if match1 && match2 {
95+
return dfs(s1, s2, s3, p1+1, p2, visited) || dfs(s1, s2, s3, p1, p2+1, visited)
96+
} else if match1 {
97+
return dfs(s1, s2, s3, p1+1, p2, visited)
98+
} else if match2 {
99+
return dfs(s1, s2, s3, p1, p2+1, visited)
100+
} else {
101+
return false
102+
}
103+
}
104+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func checkSubarraySum(nums []int, k int) bool {
4+
m := make(map[int]int)
5+
m[0] = -1
6+
sum := 0
7+
for i, n := range nums {
8+
sum += n
9+
if r, ok := m[sum%k]; ok {
10+
if i-2 >= r {
11+
return true
12+
}
13+
} else {
14+
m[sum%k] = i
15+
}
16+
}
17+
return false
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question523 struct {
9+
para523
10+
ans523
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para523 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans523 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem523(t *testing.T) {
27+
28+
qs := []question523{
29+
30+
{
31+
para523{[]int{23, 2, 4, 6, 7}, 6},
32+
ans523{true},
33+
},
34+
35+
{
36+
para523{[]int{23, 2, 6, 4, 7}, 6},
37+
ans523{true},
38+
},
39+
40+
{
41+
para523{[]int{23, 2, 6, 4, 7}, 13},
42+
ans523{false},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 523------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans523, q.para523
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, checkSubarraySum(p.nums, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [523. Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)
2+
3+
4+
## 题目
5+
6+
Given an integer array `nums` and an integer `k`, return `true` *if* `nums` *has a continuous subarray of size **at least two** whose elements sum up to a multiple of* `k`*, or* `false` *otherwise*.
7+
8+
An integer `x` is a multiple of `k` if there exists an integer `n` such that `x = n * k``0` is **always** a multiple of `k`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [23,2,4,6,7], k = 6
14+
Output: true
15+
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: nums = [23,2,6,4,7], k = 6
22+
Output: true
23+
Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
24+
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: nums = [23,2,6,4,7], k = 13
31+
Output: false
32+
```
33+
34+
**Constraints:**
35+
36+
- `1 <= nums.length <= 105`
37+
- `0 <= nums[i] <= 109`
38+
- `0 <= sum(nums[i]) <= 231 - 1`
39+
- `1 <= k <= 231 - 1`
40+
41+
## 题目大意
42+
43+
给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:
44+
45+
- 子数组大小至少为 2 ,且
46+
- 子数组元素总和为 k 的倍数。
47+
48+
如果存在,返回 true ;否则,返回 false 。如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。
49+
50+
## 解题思路
51+
52+
- 简单题。题目只要求是否存在,不要求找出所有解。用一个变量 sum 记录累加和。子数组的元素和可以用前缀和相减得到,例如 [i,j] 区间内的元素和,可以由 prefixSum[j] - prefixSum[i] 得到。当 prefixSums[j]−prefixSums[i] 为 k 的倍数时,prefixSums[i] 和 prefixSums[j] 除以 k 的余数相同。因此只需要计算每个下标对应的前缀和除以 k 的余数即可,使用 map 存储每个余数第一次出现的下标即可。在 map 中如果存在相同余数的 key,代表当前下标和 map 中这个 key 记录的下标可以满足总和为 k 的倍数这一条件。再判断一下能否满足大小至少为 2 的条件即可。用 2 个下标相减,长度大于等于 2 即满足条件,可以输出 true。
53+
54+
## 代码
55+
56+
```go
57+
package leetcode
58+
59+
func checkSubarraySum(nums []int, k int) bool {
60+
m := make(map[int]int)
61+
m[0] = -1
62+
sum := 0
63+
for i, n := range nums {
64+
sum += n
65+
if r, ok := m[sum%k]; ok {
66+
if i-2 >= r {
67+
return true
68+
}
69+
} else {
70+
m[sum%k] = i
71+
}
72+
}
73+
return false
74+
}
75+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
func findMaxLength(nums []int) int {
4+
dict := map[int]int{}
5+
dict[0] = -1
6+
count, res := 0, 0
7+
for i := 0; i < len(nums); i++ {
8+
if nums[i] == 0 {
9+
count--
10+
} else {
11+
count++
12+
}
13+
if idx, ok := dict[count]; ok {
14+
res = max(res, i-idx)
15+
} else {
16+
dict[count] = i
17+
}
18+
}
19+
return res
20+
}
21+
22+
func max(a, b int) int {
23+
if a > b {
24+
return a
25+
}
26+
return b
27+
}

0 commit comments

Comments
 (0)