10000 add 2544 · githubniraj/Leetcode@3d79d0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d79d0e

Browse files
add 2544
1 parent b7f0e20 commit 3d79d0e

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|----------------------------------|-------------
11+
| 2544 |[Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2544.java) || Easy |
1112
| 2540 |[Minimum Common Value](https://leetcode.com/problems/minimum-common-value/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2540.java) || Easy |
1213
| 2536 |[Increment Submatrices by One](https://leetcode.com/problems/increment-submatrices-by-one/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2536.java) || Medium |
1314
| 2535 |[Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2535.java) || Easy |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2544 {
4+
public static class Solution1 {
5+
public int alternateDigitSum(int n) {
6+
int result = 0;
7+
int original = n;
8+
int digits = 0;
9+
while (n != 0) {
10+
n /= 10;
11+
digits++;
12+
}
13+
boolean plus = digits % 2 != 0;
14+
while (original != 0) {
15+
int lastDigit = original % 10;
16+
if (plus) {
17+
result += lastDigit;
18+
} else {
19+
result -= lastDigit;
20+
}
21+
plus = !plus;
22+
original /= 10;
23+
}
24+
return result;
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2544;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2544Test {
10+
private static _2544.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2544.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.alternateDigitSum(521));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.alternateDigitSum(10));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
0