Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (caster )時間1年前 (2024/06/01 08:58), 編輯推噓3(300)
留言3則, 3人參與, 1年前最新討論串304/1554 (看更多)
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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.48.144 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717203497.A.51F.html

06/01 08:59, 1年前 , 1F
大師
06/01 08:59, 1F

06/01 08:59, 1年前 , 2F
別捲了
06/01 08:59, 2F

06/01 09:09, 1年前 , 3F
不是 你們都這麼早的嗎
06/01 09:09, 3F
文章代碼(AID): #1cMd8fKV (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cMd8fKV (Marginalman)