2544. Alternating Digit Sum
2023. 1. 28. 23:38ㆍAlgorithm/Leetcode, Lintcode, HackerRank, etc.
- 목차
반응형
Kotlin
class Solution {
fun alternateDigitSum(n: Int): Int {
val s = n.toString()
var sum: Int = 0
var toggle: Boolean = true
for (i in 0 until s.length) {
val digit = Character.getNumericValue(s[i])
if (true == toggle) {
sum += digit
} else {
sum += -1 * digit
}
toggle = !toggle
}
return sum
}
}
반응형
'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글
2541. Minimum Operations to Make Array Equal II (0) | 2023.01.29 |
---|---|
2545. Sort the Students by Their Kth Score (0) | 2023.01.29 |
2255. Count Prefixes of a Given String (0) | 2023.01.26 |
2269. Find the K-Beauty of a Number (0) | 2023.01.26 |
2389. Longest Subsequence With Limited Sum (0) | 2023.01.24 |