Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-06-01 08:58:14
https://leetcode.com/problems/score-of-a-string
s的分数为相邻字母ascll的绝对差的总和 求分数
Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108,
'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 -
108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.
Example 2:
Input: s = "zaz"
Output: 50
Explanation:
The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the
score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.
Constraints:
2 <= s.length <= 100
s consists only of lowercase English letters.
Python Code:
class Solution:
def scoreOfString(self, s: str) -> int:
res = 0
for i in range(1,len(s)):
res += abs(ord(s[i-1]) - ord(s[i]))
return res
作者: rainkaras (rainkaras)   2024-06-01 08:59:00
大师
作者: DJYOSHITAKA (Evans)   2024-06-01 08:59:00
别卷了
作者: sixB (6B)   2024-06-01 09:09:00
不是 你们都这么早的吗

Links booklink

Contact Us: admin [ a t ] ucptt.com