跳到主要内容

2660.保龄球游戏的获胜者

链接:2660.保龄球游戏的获胜者
难度:Easy
标签:数组、模拟
简介:返回如果玩家 1 的得分高于玩家 2 的得分,则为 1 ;如果玩家 2 的得分高于玩家 1 的得分,则为 2 ;如果平局,则为 0 。

题解 1 - python

  • 编辑时间:2023-12-27
  • 执行用时:72ms
  • 内存消耗:16.9MB
  • 编程语言:python
  • 解法介绍:模拟。
class Solution:
def isWinner(self, player1: List[int], player2: List[int]) -> int:
def getScore(player: List[int]) -> int:
cur = 0
sum = 0
for v in player:
sum += v + v * ((cur & 0b11) != 0)
cur = cur << 1 | (v == 10)
return sum
s1, s2 = getScore(player1), getScore(player2)
return 1 if s1 > s2 else 2 if s2 > s1 else 0

题解 2 - cpp

  • 编辑时间:2023-12-27
  • 执行用时:44ms
  • 内存消耗:70.5MB
  • 编程语言:cpp
  • 解法介绍:同上。
class Solution {
public:
int getScore(vector<int>& player) {
int cur = 0, sum = 0;
for (auto &v: player) {
sum += v + v * ((cur & 0b11) != 0);
cur = cur << 1 | (v == 10);
}
return sum;
}
int isWinner(vector<int>& player1, vector<int>& player2) {
int s1 = getScore(player1), s2 = getScore(player2);
return s1 > s2 ? 1 : s2 > s1 ? 2 : 0;
}
};

题解 3 - rust

  • 编辑时间:2023-12-27
  • 执行用时:12ms
  • 内存消耗:2.12MB
  • 编程语言:rust
  • 解法介绍:同上。
impl Solution {
pub fn is_winner(player1: Vec<i32>, player2: Vec<i32>) -> i32 {
fn getScore(player: &Vec<i32>) -> i32 {
let mut cur = 0;
let mut sum = 0;
for v in player {
sum += *v + *v * ((cur & 0b11) != 0) as i32;
cur = cur << 1 | (*v == 10) as i32;
}
sum
}
let (s1, s2) = (getScore(&player1), getScore(&player2));
if s1 > s2 {
1
} else if s2 > s1 {
2
} else {
0
}
}
}