Skip to content

Commit 898ac41

Browse files
committed
update: 添加问题“3110.字符串的分数”的代码和题解(#815)
test: #811 Signed-off-by: LetMeFly666 <[email protected]>
1 parent 8707d55 commit 898ac41

11 files changed

+1287
-16
lines changed

.commitmsg

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
2-
feat: `newSolution.py`commit记录prNum的预测尽量正确 (#811)
3-
测试方式:newSolutions.py -> 手动创建新issue -> 写题解并执行脚本后续
4-
5-
found: 发现了powershell的<code>`n</code>这个换行符
6-
7-
todo: rm commit2.md
8-
9-
docs: 八股/面经 - 画图有时候真的会上瘾
1+
test: #811

.nodeploy

Whitespace-only changes.

Codes/3110-score-of-a-string.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-15 10:26:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-15 10:27:59
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int scoreOfString(string s) {
14+
int ans = 0;
15+
for (int i = 1; i < s.size(); i++) {
16+
ans += abs(s[i] - s[i - 1]);
17+
}
18+
return ans;
19+
}
20+
};

Codes/3110-score-of-a-string.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-15 10:29:29
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-15 10:32:20
6+
*/
7+
package main
8+
9+
func abs3110(a int) int {
10+
if a < 0 {
11+
return -a
12+
}
13+
return a
14+
}
15+
16+
func scoreOfString(s string) (ans int) {
17+
for i := 1; i < len(s); i++ {
18+
ans += abs3110(int(s[i]) - int(s[i - 1]))
19+
}
20+
return
21+
}

Codes/3110-score-of-a-string.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-15 10:36:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-15 10:36:23
6+
*/
7+
class Solution {
8+
public int scoreOfString(String s) {
9+
int ans = 0;
10+
for (int i = 1; i < s.length(); i++) {
11+
ans += Math.abs(s.charAt(i) - s.charAt(i - 1));
12+
}
13+
return ans;
14+
}
15+
}

Codes/3110-score-of-a-string.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-15 10:28:26
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-15 10:28:50
6+
'''
7+
class Solution:
8+
def scoreOfString(self, s: str) -> int:
9+
return sum(abs(ord(s[i]) - ord(s[i - 1])) for i in range(1, len(s)))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-15 10:33:37
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-15 10:35:05
6+
*/
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
a := byte(1)
13+
b := byte(2)
14+
c := a - b
15+
fmt.Println(c) // 255
16+
d := int(c)
17+
fmt.Println(d) // 255
18+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@
862862
|3099.哈沙德数|简单|<a href="https://leetcode.cn/problems/harshad-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/03/LeetCode%203099.%E5%93%88%E6%B2%99%E5%BE%B7%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140160763" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/harshad-number/solutions/2830235/letmefly-3099ha-sha-de-shu-ji-suan-yi-ge-jqmy/" target="_blank">LeetCode题解</a>|
863863
|3101.交替子数组计数|中等|<a href="https://leetcode.cn/problems/count-alternating-subarrays/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/06/LeetCode%203101.%E4%BA%A4%E6%9B%BF%E5%AD%90%E6%95%B0%E7%BB%84%E8%AE%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140226055" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-alternating-subarrays/solutions/2832747/letmefly-3101jiao-ti-zi-shu-zu-ji-shu-de-sh58/" target="_blank">LeetCode题解</a>|
864864
|3106.满足距离约束且字典序最小的字符串|中等|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-operations-with-constraint/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/28/LeetCode%203106.%E6%BB%A1%E8%B6%B3%E8%B7%9D%E7%A6%BB%E7%BA%A6%E6%9D%9F%E4%B8%94%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140758056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-operations-with-constraint/solutions/2860931/letmefly-3106man-zu-ju-chi-yue-shu-qie-z-2wpy/" target="_blank">LeetCode题解</a>|
865+
|3110.字符串的分数|简单|<a href="https://leetcode.cn/problems/score-of-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/15/LeetCode%203110.%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146275888" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/score-of-a-string/solutions/3612406/letmefly-3110zi-fu-chuan-de-fen-shu-mo-n-aqpo/" target="_blank">LeetCode题解</a>|
865866
|3112.访问消失节点的最少时间|中等|<a href="https://leetcode.cn/problems/minimum-time-to-visit-disappearing-nodes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/18/LeetCode%203112.%E8%AE%BF%E9%97%AE%E6%B6%88%E5%A4%B1%E8%8A%82%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140530368" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-time-to-visit-disappearing-nodes/solutions/2848666/letmefly-3112fang-wen-xiao-shi-jie-dian-1cgwc/" target="_blank">LeetCode题解</a>|
866867
|3115.质数的最大距离|中等|<a href="https://leetcode.cn/problems/maximum-prime-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/02/LeetCode%203115.%E8%B4%A8%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E8%B7%9D%E7%A6%BB/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140121329" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-prime-difference/solutions/2828631/letmefly-3115zhi-shu-de-zui-da-ju-chi-zh-1yvd/" target="_blank">LeetCode题解</a>|
867868
|3127.构造相同颜色的正方形|简单|<a href="https://leetcode.cn/problems/make-a-square-with-the-same-color/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/08/31/LeetCode%203127.%E6%9E%84%E9%80%A0%E7%9B%B8%E5%90%8C%E9%A2%9C%E8%89%B2%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141756605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/make-a-square-with-the-same-color/solutions/2899199/letmefly-3127gou-zao-xiang-tong-yan-se-d-xp4l/" target="_blank">LeetCode题解</a>|
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: 3110.字符串的分数:模拟(注意一个小细节)
3+
date: 2025-03-15 10:44:37
4+
tags: [题解, LeetCode, 简单, 字符串]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3110.字符串的分数:模拟(注意一个小细节)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/score-of-a-string/](https://leetcode.cn/problems/score-of-a-string/)
11+
12+
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;。一个字符串的&nbsp;<strong>分数</strong>&nbsp;定义为相邻字符 <strong>ASCII</strong>&nbsp;码差值绝对值的和。</p>
13+
14+
<p>请你返回 <code>s</code>&nbsp;的 <strong>分数</strong>&nbsp;。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong class="example">示例 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><span class="example-io"><b>输入:</b>s = "hello"</span></p>
22+
23+
<p><span class="example-io"><b>输出:</b>13</span></p>
24+
25+
<p><strong>解释:</strong></p>
26+
27+
<p><code>s</code>&nbsp;中字符的 <strong>ASCII </strong>码分别为:<code>'h' = 104</code>&nbsp;,<code>'e' = 101</code>&nbsp;,<code>'l' = 108</code>&nbsp;,<code>'o' = 111</code>&nbsp;。所以&nbsp;<code>s</code>&nbsp;的分数为&nbsp;<code>|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13</code>&nbsp;。</p>
28+
</div>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><span class="example-io"><b>输入:</b>s = "zaz"</span></p>
34+
35+
<p><span class="example-io"><b>输出:</b>50</span></p>
36+
37+
<p><strong>解释:</strong></p>
38+
39+
<p><code>s</code>&nbsp;中字符的 <strong>ASCII&nbsp;</strong>码分别为:<code>'z' = 122</code>&nbsp;,<code>'a' = 97</code>&nbsp;。所以&nbsp;<code>s</code>&nbsp;的分数为&nbsp;<code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code>&nbsp;。</p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
48+
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
49+
</ul>
50+
51+
52+
53+
## 解题方法:模拟
54+
55+
直接从第二个字符开始遍历字符串,将这个字符减去前一个字符的绝对值累加到答案中,最终返回即可。
56+
57+
注意:请谨慎使用8比特数减8比特数(如char - char或byte - byte),因为有的编程语言中8比特数相减不会**类型提升**转为int相减。
58+
59+
如下代码的运行结果为255和255:
60+
61+
```go
62+
package main
63+
64+
import "fmt"
65+
66+
func main() {
67+
a := byte(1)
68+
b := byte(2)
69+
c := a - b
70+
fmt.Println(c) // 255
71+
d := int(c)
72+
fmt.Println(d) // 255
73+
}
74+
```
75+
76+
+ 时间复杂度$O(len(s))$
77+
+ 空间复杂度$O(1)$
78+
79+
### AC代码
80+
81+
#### C++
82+
83+
```cpp
84+
/*
85+
* @Author: LetMeFly
86+
* @Date: 2025-03-15 10:26:54
87+
* @LastEditors: LetMeFly.xyz
88+
* @LastEditTime: 2025-03-15 10:27:59
89+
*/
90+
#ifdef _WIN32
91+
#include "_[1,2]toVector.h"
92+
#endif
93+
94+
class Solution {
95+
public:
96+
int scoreOfString(string s) {
97+
int ans = 0;
98+
for (int i = 1; i < s.size(); i++) {
99+
ans += abs(s[i] - s[i - 1]);
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
#### Python
107+
108+
```python
109+
'''
110+
Author: LetMeFly
111+
Date: 2025-03-15 10:28:26
112+
LastEditors: LetMeFly.xyz
113+
LastEditTime: 2025-03-15 10:28:50
114+
'''
115+
class Solution:
116+
def scoreOfString(self, s: str) -> int:
117+
return sum(abs(ord(s[i]) - ord(s[i - 1])) for i in range(1, len(s)))
118+
```
119+
120+
#### Java
121+
122+
```java
123+
/*
124+
* @Author: LetMeFly
125+
* @Date: 2025-03-15 10:36:15
126+
* @LastEditors: LetMeFly.xyz
127+
* @LastEditTime: 2025-03-15 10:36:23
128+
*/
129+
class Solution {
130+
public int scoreOfString(String s) {
131+
int ans = 0;
132+
for (int i = 1; i < s.length(); i++) {
133+
ans += Math.abs(s.charAt(i) - s.charAt(i - 1));
134+
}
135+
return ans;
136+
}
137+
}
138+
```
139+
140+
#### Go
141+
142+
```go
143+
/*
144+
* @Author: LetMeFly
145+
* @Date: 2025-03-15 10:29:29
146+
* @LastEditors: LetMeFly.xyz
147+
* @LastEditTime: 2025-03-15 10:32:20
148+
*/
149+
package main
150+
151+
func abs3110(a int) int {
152+
if a < 0 {
153+
return -a
154+
}
155+
return a
156+
}
157+
158+
func scoreOfString(s string) (ans int) {
159+
for i := 1; i < len(s); i++ {
160+
ans += abs3110(int(s[i]) - int(s[i - 1]))
161+
}
162+
return
163+
}
164+
```
165+
166+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/146275888)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/03/15/LeetCode%203110.%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%88%86%E6%95%B0/)哦~
167+
>
168+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)