Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afd76a0

Browse files
feat: add solutions to lc problem: No.3110 (doocs#4160)
No.3110.Score of a String
1 parent 403e8ef commit afd76a0

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

‎solution/3100-3199/3110.Score of a String/README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,52 @@ function scoreOfString(s: string): number {
138138
}
139139
```
140140

141+
#### Rust
142+
143+
```rust
144+
impl Solution {
145+
pub fn score_of_string(s: String) -> i32 {
146+
s.as_bytes()
147+
.windows(2)
148+
.map(|w| (w[0] as i32 - w[1] as i32).abs())
149+
.sum()
150+
}
151+
}
152+
```
153+
154+
#### C#
155+
156+
```cs
157+
public class Solution {
158+
public int ScoreOfString(string s) {
159+
int ans = 0;
160+
for (int i = 1; i < s.Length; ++i) {
161+
ans += Math.Abs(s[i] - s[i - 1]);
162+
}
163+
return ans;
164+
}
165+
}
166+
```
167+
168+
#### PHP
169+
170+
```php
171+
class Solution {
172+
/**
173+
* @param String $s
174+
* @return Integer
175+
*/
176+
function scoreOfString($s) {
177+
$ans = 0;
178+
$n = strlen($s);
179+
for ($i = 1; $i < $n; ++$i) {
180+
$ans += abs(ord($s[$i]) - ord($s[$i - 1]));
181+
}
182+
return $ans;
183+
}
184+
}
185+
```
186+
141187
<!-- tabs:end -->
142188

143189
<!-- solution:end -->

‎solution/3100-3199/3110.Score of a String/README_EN.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,52 @@ function scoreOfString(s: string): number {
136136
}
137137
```
138138

139+
#### Rust
140+
141+
```rust
142+
impl Solution {
143+
pub fn score_of_string(s: String) -> i32 {
144+
s.as_bytes()
145+
.windows(2)
146+
.map(|w| (w[0] as i32 - w[1] as i32).abs())
147+
.sum()
148+
}
149+
}
150+
```
151+
152+
#### C#
153+
154+
```cs
155+
public class Solution {
156+
public int ScoreOfString(string s) {
157+
int ans = 0;
158+
for (int i = 1; i < s.Length; ++i) {
159+
ans += Math.Abs(s[i] - s[i - 1]);
160+
}
161+
return ans;
162+
}
163+
}
164+
```
165+
166+
#### PHP
167+
168+
```php
169+
class Solution {
170+
/**
171+
* @param String $s
172+
* @return Integer
173+
*/
174+
function scoreOfString($s) {
175+
$ans = 0;
176+
$n = strlen($s);
177+
for ($i = 1; $i < $n; ++$i) {
178+
$ans += abs(ord($s[$i]) - ord($s[$i - 1]));
179+
}
180+
return $ans;
181+
}
182+
}
183+
```
184+
139185
<!-- tabs:end -->
140186

141187
<!-- solution:end -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Solution {
2+
public int ScoreOfString(string s) {
3+
int ans = 0;
4+
for (int i = 1; i < s.Length; ++i) {
5+
ans += Math.Abs(s[i] - s[i - 1]);
6+
}
7+
return ans;
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
/**
3+
* @param String $s
4+
* @return Integer
5+
*/
6+
function scoreOfString($s) {
7+
$ans = 0;
8+
$n = strlen($s);
9+
for ($i = 1; $i < $n; ++$i) {
10+
$ans += abs(ord($s[$i]) - ord($s[$i - 1]));
11+
}
12+
return $ans;
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn score_of_string(s: String) -> i32 {
3+
s.as_bytes()
4+
.windows(2)
5+
.map(|w| (w[0] as i32 - w[1] as i32).abs())
6+
.sum()
7+
}
8+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /