1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-04 14:40:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/字符串的分数 [score-of-a-string].html

39 lines
1.6 KiB
HTML
Raw Normal View History

2024-04-30 10:04:49 +08:00
<p>给你一个字符串&nbsp;<code>s</code>&nbsp;。一个字符串的&nbsp;<strong>分数</strong>&nbsp;定义为相邻字符 <strong>ASCII</strong>&nbsp;码差值绝对值的和。</p>
<p>请你返回 <code>s</code>&nbsp;<strong>分数</strong>&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "hello"</span></p>
<p><span class="example-io"><b>输出:</b>13</span></p>
<p><strong>解释:</strong></p>
<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>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>s = "zaz"</span></p>
<p><span class="example-io"><b>输出:</b>50</span></p>
<p><strong>解释:</strong></p>
<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>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code>&nbsp;只包含小写英文字母。</li>
</ul>