Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c99bf39

Browse files
committed
添加 0474.一和零.md Scala版本
1 parent a1e9f25 commit c99bf39

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

‎problems/0474.一和零.md

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public:
163163
## 其他语言版本
164164
165165
166-
Java:
166+
### Java
167167
```Java
168168
class Solution {
169169
public int findMaxForm(String[] strs, int m, int n) {
@@ -192,7 +192,7 @@ class Solution {
192192
}
193193
```
194194

195-
Python:
195+
### Python
196196
```python
197197
class Solution:
198198
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
@@ -208,7 +208,7 @@ class Solution:
208208
return dp[m][n]
209209
```
210210

211-
Go:
211+
### Go
212212
```go
213213
func findMaxForm(strs []string, m int, n int) int {
214214
// 定义数组
@@ -294,7 +294,7 @@ func getMax(a,b int)int{
294294
}
295295
```
296296
297-
Javascript:
297+
### Javascript
298298
```javascript
299299
const findMaxForm = (strs, m, n) => {
300300
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
@@ -323,7 +323,7 @@ const findMaxForm = (strs, m, n) => {
323323
};
324324
```
325325

326-
TypeScript:
326+
### TypeScript
327327

328328
> 滚动数组,二维数组法
329329
@@ -446,7 +446,80 @@ function isValidSubSet(strs: string[], m: number, n: number): boolean {
446446
}
447447
```
448448

449+
### Scala
449450

451+
背包:
452+
```scala
453+
object Solution {
454+
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
455+
var dp = Array.ofDim[Int](m + 1, n + 1)
456+
457+
var (oneNum, zeroNum) = (0, 0)
458+
459+
for (str <- strs) {
460+
oneNum = 0
461+
zeroNum = 0
462+
for (i <- str.indices) {
463+
if (str(i) == '0') zeroNum += 1
464+
else oneNum += 1
465+
}
466+
467+
for (i <- m to zeroNum by -1) {
468+
for (j <- n to oneNum by -1) {
469+
dp(i)(j) = math.max(dp(i)(j), dp(i - zeroNum)(j - oneNum) + 1)
470+
}
471+
}
472+
}
473+
474+
dp(m)(n)
475+
}
476+
}
477+
```
478+
479+
回溯法(超时):
480+
```scala
481+
object Solution {
482+
import scala.collection.mutable
483+
484+
var res = Int.MinValue
485+
486+
def test(str: String): (Int, Int) = {
487+
var (zero, one) = (0, 0)
488+
for (i <- str.indices) {
489+
if (str(i) == '1') one += 1
490+
else zero += 1
491+
}
492+
(zero, one)
493+
}
494+
495+
def travsel(strs: Array[String], path: mutable.ArrayBuffer[String], m: Int, n: Int, startIndex: Int): Unit = {
496+
if (startIndex > strs.length) {
497+
return
498+
}
499+
500+
res = math.max(res, path.length)
501+
502+
for (i <- startIndex until strs.length) {
503+
504+
var (zero, one) = test(strs(i))
505+
506+
// 如果0的个数小于m,1的个数小于n,则可以回溯
507+
if (zero <= m && one <= n) {
508+
path.append(strs(i))
509+
travsel(strs, path, m - zero, n - one, i + 1)
510+
path.remove(path.length - 1)
511+
}
512+
}
513+
}
514+
515+
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
516+
res = Int.MinValue
517+
var path = mutable.ArrayBuffer[String]()
518+
travsel(strs, path, m, n, 0)
519+
res
520+
}
521+
}
522+
```
450523

451524
-----------------------
452525
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /