Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- range
- 파이썬
- split
- DART
- Python
- FOR
- count
- Sorted
- or
- join
- map
- index
- If
- 위코드
- WECODE
- lambda
- enumerate
- Algorithm
- and
- js
- len
- sum
- STR
- list
- SQL
- MAX
- LOWER
- decorator
- slice
- iNT
Archives
- Today
- Total
코드로 우주평화
1206. Sum of positive 본문
ou get an array of numbers, return the sum of all of the positives ones.
Example [1,-4,7,12] => 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum is default to 0.
Solution:
int positiveSum(List<int> arr) {
int result = 0;
for (int a in arr) {
if (a > 0) {
result += a;
}
}
return result;
}
int positiveSum(List<int> arr) {
return arr.where((l) => l > 0).fold(0, (p, c) => p + c);
}
import "dart:math";
int positiveSum(List<int> xs) {
return xs.fold(0, (a, x) => a + max(x, 0));
}
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1208. A Strange Trip to the Market (0) | 2022.12.08 |
---|---|
1207. Vowel Count (0) | 2022.12.07 |
1205. Sum of Cubes (0) | 2022.12.05 |
1204. Odd or Even? (0) | 2022.12.04 |
1203. String repeat (0) | 2022.12.04 |