跳转至

LCP 17. 速算机器人

题目描述

小扣在秋日市集发现了一款速算机器人。店家对机器人说出两个数字(记作 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' 组成

解法

方法一

1
2
3
4
5
6
7
8
9
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    func calculate(_ s: String) -> Int {
        var x = 1
        var y = 0
        for c in s {
            if c == "A" {
                x = x * 2 + y
            } else if c == "B" {
                y = y * 2 + x
            }
        }
        return x + y
    }
}

评论