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 4cb3897

Browse files
committed
添加 0494.目标和.md Scala版本
1 parent f1fb80d commit 4cb3897

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

‎problems/0494.目标和.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ dp[j] += dp[j - nums[i]];
250250
## 其他语言版本
251251
252252
253-
Java:
253+
### Java
254254
```java
255255
class Solution {
256256
public int findTargetSumWays(int[] nums, int target) {
@@ -271,7 +271,7 @@ class Solution {
271271
}
272272
```
273273

274-
Python:
274+
### Python
275275
```python
276276
class Solution:
277277
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@@ -287,7 +287,7 @@ class Solution:
287287
return dp[bagSize]
288288
```
289289

290-
Go:
290+
### Go
291291
```go
292292
func findTargetSumWays(nums []int, target int) int {
293293
sum := 0
@@ -322,7 +322,7 @@ func abs(x int) int {
322322
}
323323
```
324324

325-
Javascript:
325+
### Javascript
326326
```javascript
327327
const findTargetSumWays = (nums, target) => {
328328

@@ -351,7 +351,7 @@ const findTargetSumWays = (nums, target) => {
351351
};
352352
```
353353

354-
TypeScript:
354+
### TypeScript
355355

356356
```typescript
357357
function findTargetSumWays(nums: number[], target: number): number {
@@ -370,7 +370,25 @@ function findTargetSumWays(nums: number[], target: number): number {
370370
};
371371
```
372372

373+
### Scala
374+
375+
```scala
376+
object Solution {
377+
def findTargetSumWays(nums: Array[Int], target: Int): Int = {
378+
var sum = nums.sum
379+
if (math.abs(target) > sum) return 0 // 此时没有方案
380+
if ((sum + target) % 2 == 1) return 0 // 此时没有方案
381+
var bagSize = (sum + target) / 2
382+
var dp = new Array[Int](bagSize + 1)
383+
dp(0) = 1
384+
for (i <- 0 until nums.length; j <- bagSize to nums(i) by -1) {
385+
dp(j) += dp(j - nums(i))
386+
}
373387

388+
dp(bagSize)
389+
}
390+
}
391+
```
374392

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

0 commit comments

Comments
(0)

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