|
| 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>给你一个字符串 <code>s</code> 。一个字符串的 <strong>分数</strong> 定义为相邻字符 <strong>ASCII</strong> 码差值绝对值的和。</p> |
| 13 | + |
| 14 | +<p>请你返回 <code>s</code> 的 <strong>分数</strong> 。</p> |
| 15 | + |
| 16 | +<p> </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> 中字符的 <strong>ASCII </strong>码分别为:<code>'h' = 104</code> ,<code>'e' = 101</code> ,<code>'l' = 108</code> ,<code>'o' = 111</code> 。所以 <code>s</code> 的分数为 <code>|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13</code> 。</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> 中字符的 <strong>ASCII </strong>码分别为:<code>'z' = 122</code> ,<code>'a' = 97</code> 。所以 <code>s</code> 的分数为 <code>|122 - 97| + |97 - 122| = 25 + 25 = 50</code> 。</p> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | + |
| 44 | +<p><strong>提示:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>2 <= s.length <= 100</code></li> |
| 48 | + <li><code>s</code> 只包含小写英文字母。</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