Skip to content

Commit ea495cd

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 7296540 + 651e3ea commit ea495cd

File tree

323 files changed

+40595
-28825
lines changed

Some content is hidden

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

323 files changed

+40595
-28825
lines changed

images/starcharts.svg

Lines changed: 25672 additions & 25571 deletions
Loading

lcof2/剑指 Offer II 101. 分割等和子串/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ func canPartition(nums []int) bool {
156156
}
157157
```
158158

159+
#### Swift
160+
161+
```swift
162+
class Solution {
163+
func canPartition(_ nums: [Int]) -> Bool {
164+
let s = nums.reduce(0, +)
165+
if s % 2 != 0 { return false }
166+
let target = s / 2
167+
var dp = Array(repeating: false, count: target + 1)
168+
dp[0] = true
169+
170+
for num in nums {
171+
for j in stride(from: target, through: num, by: -1) {
172+
dp[j] = dp[j] || dp[j - num]
173+
}
174+
}
175+
176+
return dp[target]
177+
}
178+
}
179+
```
180+
159181
<!-- tabs:end -->
160182

161183
<!-- solution:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func canPartition(_ nums: [Int]) -> Bool {
3+
let s = nums.reduce(0, +)
4+
if s % 2 != 0 { return false }
5+
let target = s / 2
6+
var dp = Array(repeating: false, count: target + 1)
7+
dp[0] = true
8+
9+
for num in nums {
10+
for j in stride(from: target, through: num, by: -1) {
11+
dp[j] = dp[j] || dp[j - num]
12+
}
13+
}
14+
15+
return dp[target]
16+
}
17+
}

lcof2/剑指 Offer II 102. 加减的目标值/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,35 @@ func findTargetSumWays(nums []int, target int) int {
163163
}
164164
```
165165

166+
#### Swift
167+
168+
```swift
169+
class Solution {
170+
func findTargetSumWays(_ nums: [Int], _ target: Int) -> Int {
171+
if target < -1000 || target > 1000 {
172+
return 0
173+
}
174+
175+
let n = nums.count
176+
var dp = Array(repeating: Array(repeating: 0, count: 2001), count: n)
177+
178+
dp[0][nums[0] + 1000] += 1
179+
dp[0][-nums[0] + 1000] += 1
180+
181+
for i in 1..<n {
182+
for j in -1000...1000 {
183+
if dp[i - 1][j + 1000] > 0 {
184+
dp[i][j + nums[i] + 1000] += dp[i - 1][j + 1000]
185+
dp[i][j - nums[i] + 1000] += dp[i - 1][j + 1000]
186+
}
187+
}
188+
}
189+
190+
return dp[n - 1][target + 1000]
191+
}
192+
}
193+
```
194+
166195
<!-- tabs:end -->
167196

168197
<!-- solution:end -->
@@ -241,6 +270,29 @@ func findTargetSumWays(nums []int, target int) int {
241270
}
242271
```
243272

273+
#### Swift
274+
275+
```swift
276+
class Solution {
277+
func findTargetSumWays(_ nums: [Int], _ target: Int) -> Int {
278+
let s = nums.reduce(0, +)
279+
if s - target < 0 || (s - target) % 2 != 0 {
280+
return 0
281+
}
282+
let target = (s - target) / 2
283+
var dp = [Int](repeating: 0, count: target + 1)
284+
dp[0] = 1
285+
286+
for num in nums {
287+
for j in stride(from: target, through: num, by: -1) {
288+
dp[j] += dp[j - num]
289+
}
290+
}
291+
return dp[target]
292+
}
293+
}
294+
```
295+
244296
<!-- tabs:end -->
245297

246298
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func findTargetSumWays(_ nums: [Int], _ target: Int) -> Int {
3+
if target < -1000 || target > 1000 {
4+
return 0
5+
}
6+
7+
let n = nums.count
8+
var dp = Array(repeating: Array(repeating: 0, count: 2001), count: n)
9+
10+
dp[0][nums[0] + 1000] += 1
11+
dp[0][-nums[0] + 1000] += 1
12+
13+
for i in 1..<n {
14+
for j in -1000...1000 {
15+
if dp[i - 1][j + 1000] > 0 {
16+
dp[i][j + nums[i] + 1000] += dp[i - 1][j + 1000]
17+
dp[i][j - nums[i] + 1000] += dp[i - 1][j + 1000]
18+
}
19+
}
20+
}
21+
22+
return dp[n - 1][target + 1000]
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
func findTargetSumWays(_ nums: [Int], _ target: Int) -> Int {
3+
let s = nums.reduce(0, +)
4+
if s - target < 0 || (s - target) % 2 != 0 {
5+
return 0
6+
}
7+
let target = (s - target) / 2
8+
var dp = [Int](repeating: 0, count: target + 1)
9+
dp[0] = 1
10+
11+
for num in nums {
12+
for j in stride(from: target, through: num, by: -1) {
13+
dp[j] += dp[j - num]
14+
}
15+
}
16+
return dp[target]
17+
}
18+
}

lcof2/剑指 Offer II 103. 最少的硬币数目/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ var coinChange = function (coins, amount) {
169169
};
170170
```
171171

172+
#### Swift
173+
174+
```swift
175+
class Solution {
176+
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
177+
var dp = [Int](repeating: amount + 1, count: amount + 1)
178+
dp[0] = 0
179+
180+
for coin in coins {
181+
if coin > amount { continue }
182+
for j in coin...amount {
183+
dp[j] = min(dp[j], dp[j - coin] + 1)
184+
}
185+
}
186+
187+
return dp[amount] > amount ? -1 : dp[amount]
188+
}
189+
}
190+
```
191+
172192
<!-- tabs:end -->
173193

174194
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
3+
var dp = [Int](repeating: amount + 1, count: amount + 1)
4+
dp[0] = 0
5+
6+
for coin in coins {
7+
if coin > amount { continue }
8+
for j in coin...amount {
9+
dp[j] = min(dp[j], dp[j - coin] + 1)
10+
}
11+
}
12+
13+
return dp[amount] > amount ? -1 : dp[amount]
14+
}
15+
}

lcof2/剑指 Offer II 104. 排列的数目/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ func combinationSum4(nums []int, target int) int {
141141
}
142142
```
143143

144+
#### Swift
145+
146+
```swift
147+
class Solution {
148+
func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
149+
var dp = [Int](repeating: 0, count: target + 1)
150+
dp[0] = 1
151+
152+
for i in 1...target {
153+
for num in nums {
154+
if i >= num, dp[i] <= Int.max - dp[i - num] {
155+
dp[i] += dp[i - num]
156+
}
157+
}
158+
}
159+
160+
return dp[target]
161+
}
162+
}
163+
```
164+
144165
<!-- tabs:end -->
145166

146167
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
3+
var dp = [Int](repeating: 0, count: target + 1)
4+
dp[0] = 1
5+
6+
for i in 1...target {
7+
for num in nums {
8+
if i >= num, dp[i] <= Int.max - dp[i - num] {
9+
dp[i] += dp[i - num]
10+
}
11+
}
12+
}
13+
14+
return dp[target]
15+
}
16+
}

0 commit comments

Comments
 (0)