Skip to content

Commit cc38766

Browse files
Merge pull request #25 from fluttermiddlepodcast/2544_Alternating_Digit_Sum
added "2544. Alternating Digit Sum"
2 parents 2159316 + 7079b9b commit cc38766

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Solutions from [LeetCode](https://leetcode.com) on Dart.
2121
| 2239. Find Closest Number to Zero | [Link](https://leetcode.com/problems/find-closest-number-to-zero/) | [Link](./lib/easy/find_closest_number_to_zero.dart) |
2222
| 2481. Minimum Cuts to Divide a Circle | [Link](https://leetcode.com/problems/minimum-cuts-to-divide-a-circle/) | [Link](./lib/easy/minimum_cuts_to_divide_a_circle.dart) |
2323
| 2490. Circular Sentence | [Link](https://leetcode.com/problems/circular-sentence/) | [Link](./lib/easy/circular_sentence.dart) |
24+
| 2544. Alternating Digit Sum | [Link](https://leetcode.com/problems/alternating-digit-sum/) | [Link](./lib/easy/alternating_digit_sum.dart) |
2425

2526
## Medium
2627

lib/easy/alternating_digit_sum.dart

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
int alternateDigitSum(int n) {
3+
int result = 0;
4+
int factor = 1;
5+
final str = n.toString();
6+
for (int i = 0; i < str.length; i++) {
7+
result += int.parse(str[i]) * factor;
8+
factor *= -1;
9+
}
10+
return result;
11+
}
12+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:leetcode_dart/easy/alternating_digit_sum.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group(
6+
'Example tests',
7+
() {
8+
final ads = Solution();
9+
test('4', () => expect(4, ads.alternateDigitSum(521)));
10+
test('1', () => expect(1, ads.alternateDigitSum(111)));
11+
test('0', () => expect(0, ads.alternateDigitSum(886996)));
12+
},
13+
);
14+
}

0 commit comments

Comments
 (0)