mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加 0474.一和零.md Scala版本
This commit is contained in:
@ -163,7 +163,7 @@ public:
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
### Java
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int findMaxForm(String[] strs, int m, int n) {
|
public int findMaxForm(String[] strs, int m, int n) {
|
||||||
@ -192,7 +192,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
### Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
|
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
|
||||||
@ -208,7 +208,7 @@ class Solution:
|
|||||||
return dp[m][n]
|
return dp[m][n]
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go
|
||||||
```go
|
```go
|
||||||
func findMaxForm(strs []string, m int, n int) int {
|
func findMaxForm(strs []string, m int, n int) int {
|
||||||
// 定义数组
|
// 定义数组
|
||||||
@ -294,7 +294,7 @@ func getMax(a,b int)int{
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
### Javascript
|
||||||
```javascript
|
```javascript
|
||||||
const findMaxForm = (strs, m, n) => {
|
const findMaxForm = (strs, m, n) => {
|
||||||
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
|
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
|
||||||
@ -323,7 +323,7 @@ const findMaxForm = (strs, m, n) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript
|
||||||
|
|
||||||
> 滚动数组,二维数组法
|
> 滚动数组,二维数组法
|
||||||
|
|
||||||
@ -446,7 +446,80 @@ function isValidSubSet(strs: string[], m: number, n: number): boolean {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
背包:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
|
||||||
|
var dp = Array.ofDim[Int](m + 1, n + 1)
|
||||||
|
|
||||||
|
var (oneNum, zeroNum) = (0, 0)
|
||||||
|
|
||||||
|
for (str <- strs) {
|
||||||
|
oneNum = 0
|
||||||
|
zeroNum = 0
|
||||||
|
for (i <- str.indices) {
|
||||||
|
if (str(i) == '0') zeroNum += 1
|
||||||
|
else oneNum += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i <- m to zeroNum by -1) {
|
||||||
|
for (j <- n to oneNum by -1) {
|
||||||
|
dp(i)(j) = math.max(dp(i)(j), dp(i - zeroNum)(j - oneNum) + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dp(m)(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
回溯法(超时):
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
|
||||||
|
var res = Int.MinValue
|
||||||
|
|
||||||
|
def test(str: String): (Int, Int) = {
|
||||||
|
var (zero, one) = (0, 0)
|
||||||
|
for (i <- str.indices) {
|
||||||
|
if (str(i) == '1') one += 1
|
||||||
|
else zero += 1
|
||||||
|
}
|
||||||
|
(zero, one)
|
||||||
|
}
|
||||||
|
|
||||||
|
def travsel(strs: Array[String], path: mutable.ArrayBuffer[String], m: Int, n: Int, startIndex: Int): Unit = {
|
||||||
|
if (startIndex > strs.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res = math.max(res, path.length)
|
||||||
|
|
||||||
|
for (i <- startIndex until strs.length) {
|
||||||
|
|
||||||
|
var (zero, one) = test(strs(i))
|
||||||
|
|
||||||
|
// 如果0的个数小于m,1的个数小于n,则可以回溯
|
||||||
|
if (zero <= m && one <= n) {
|
||||||
|
path.append(strs(i))
|
||||||
|
travsel(strs, path, m - zero, n - one, i + 1)
|
||||||
|
path.remove(path.length - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
|
||||||
|
res = Int.MinValue
|
||||||
|
var path = mutable.ArrayBuffer[String]()
|
||||||
|
travsel(strs, path, m, n, 0)
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user