Skip to content

Files

Latest commit

c29b144 · May 17, 2024

History

History
125 lines (90 loc) · 2.16 KB

README.md

File metadata and controls

125 lines (90 loc) · 2.16 KB
comments edit_url
true

题目描述

小扣在秋日市集发现了一款速算机器人。店家对机器人说出两个数字(记作 xy),请小扣说出计算指令:

  • "A" 运算:使 x = 2 * x + y

  • "B" 运算:使 y = 2 * y + x

在本次游戏中,店家说出的数字为 x = 1y = 0,小扣说出的计算指令记作仅由大写字母 AB 组成的字符串 s,字符串中字符的顺序表示计算顺序,请返回最终 xy 的和为多少。

示例 1:

输入:s = "AB"

输出:4

解释:

经过一次 A 运算后,x = 2, y = 0。

再经过一次 B 运算,x = 2, y = 2。

最终 x 与 y 之和为 4。

提示:

  • 0 <= s.length <= 10
  • s'A''B' 组成

解法

方法一

Python3

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

Java

class Solution {
    public int calculate(String s) {
        int x = 1, y = 0;
        for (char c : s.toCharArray()) {
            if (c == 'A') {
                x = x * 2 + y;
            } else {
                y = y * 2 + x;
            }
        }
        return x + y;
    }
}

C++

class Solution {
public:
    int calculate(string s) {
        int x = 1, y = 0;
        for (char& c : s) {
            if (c == 'A')
                x = x * 2 + y;
            else
                y = y * 2 + x;
        }
        return x + y;
    }
};

Go

func calculate(s string) int {
	x, y := 1, 0
	for _, c := range s {
		if c == 'A' {
			x = x*2 + y
		} else {
			y = y*2 + x
		}
	}
	return x + y
}