1. 程式人生 > 實用技巧 >ARC104 題解

ARC104 題解

小扣在秋日市集發現了一款速算機器人。店家對機器人說出兩個數字(記作 x 和 y),請小扣說出計算指令:

"A" 運算:使 x = 2 * x + y;
"B" 運算:使 y = 2 * y + x。
在本次遊戲中,店家說出的數字為 x = 1 和 y = 0,小扣說出的計算指令記作僅由大寫字母 A、B 組成的字串 s,字串中字元的順序表示計算順序,請返回最終 x 與 y 的和為多少。

示例 1:

輸入:s = "AB"

輸出:4

解釋:
經過一次 A 運算後,x = 2, y = 0。
再經過一次 B 運算,x = 2, y = 2。
最終 x 與 y 之和為 4。

提示:

0 <= s.length <= 10
s 由 'A' 和 'B' 組成

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/nGK0Fy

class Solution:
    def calculate(self, s: str) -> int:
        x=1
        y=0
        for i in s:
            if i=='A':x=x*2+y
            if i=='B':y=y*2+x
        return x+y

class Solution:
    def calculate(self, s: str) -> int:
        
return 1<<len(s)