Skip to content

Commit 1470e0a

Browse files
authored
feat: add rust solution to lc problem: No.2544 (#1234)
1 parent c17cd02 commit 1470e0a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

solution/2500-2599/2544.Alternating Digit Sum/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ impl Solution {
166166
}
167167
```
168168

169+
```rust
170+
impl Solution {
171+
pub fn alternate_digit_sum(n: i32) -> i32 {
172+
let mut ans = 0;
173+
let mut sign = 1;
174+
175+
for c in format!("{}", n).chars() {
176+
let x = c.to_digit(10).unwrap()as i32;
177+
ans += x * sign;
178+
sign *= -1;
179+
}
180+
181+
ans
182+
}
183+
}
184+
```
185+
169186
### **C**
170187

171188
```c

solution/2500-2599/2544.Alternating Digit Sum/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ impl Solution {
156156
}
157157
```
158158

159+
```rust
160+
impl Solution {
161+
pub fn alternate_digit_sum(n: i32) -> i32 {
162+
let mut ans = 0;
163+
let mut sign = 1;
164+
165+
for c in format!("{}", n).chars() {
166+
let x = c.to_digit(10).unwrap()as i32;
167+
ans += x * sign;
168+
sign *= -1;
169+
}
170+
171+
ans
172+
}
173+
}
174+
```
175+
159176
### **C**
160177

161178
```c

0 commit comments

Comments
 (0)