657. Judge Route Circle(sync_with_stdio)加速

第一次用 CSDN 写博客哈,想把自己刷 leetcode 的所思所想所获得的东西写在一个地方,那就简单粗暴一点,用 CSDN 吧。

657. Judge Route Circle

leetcode 的第 657 题

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.
Example 1:
Input: “UD”
Output: true
Example 2:
Input: “LL”
Output: false

是很简单的题目,但是把代码提交后发现速度不是很快,有好多大佬速度很快,在6ms完成,而我居然要30来ms。


class Solution {
public:
    bool judgeCircle(string moves) {
        int L = 0, U = 0;
        for (int i = 0; i < moves.length(); i++)
        {
            if (moves[i] == 'L')
                L++;
            else if (moves[i] == 'R')
                L--;
            else if (moves[i] == 'U')
                U++;
            else if (moves[i] == 'D')
                U--;
        }
        if (L == 0 && U == 0)
            return true;
        else
            return false;
    }
};

加了一段优化后

static string moves =[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return "";
}();

class Solution {
public:
    bool judgeCircle(string moves) {
        int L = 0, U = 0;
        for (int i = 0; i < moves.length(); i++)
        {
            if (moves[i] == 'L')
                L++;
            else if (moves[i] == 'R')
                L--;
            else if (moves[i] == 'U')
                U++;
            else if (moves[i] == 'D')
                U--;
        }
        if (L == 0 && U == 0)
            return true;
        else
            return false;
    }
};
std::ios::sync_with_stdio(false);
cin.tie(NULL);

之前写的题目是用 cout 和 cin ,会 Timeout,如果用 printf 和 scanf 的话就能完成 AC 。之前只觉得cout cin 慢,原来是可以用这玩意加速的呀。

记住!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
def __next_step(self, x, y): if not self.judge_colory: self.__history += 0 else: self.__history += 1 self.color = 1 if self.__history % 2 == 0 else 2 if self.start_ai_game: if self.ai_color == self.color: row,col = self.ai_stage(self.ai_game()[0],self.ai_game()[1]) else: col = round((x-self.__margin*2)/self.__cell_width) row = round((y-self.__margin*2)/self.__cell_width) stage_row = (y-self.__margin)-(self.__cell_width*row+self.__margin) stage_col = (x-self.__margin)-(self.__cell_width*col+self.__margin) if stage_col < stage_row: self.direct= 1 else: self.direct= 0 else: col = round((x - self.__margin * 2) / self.__cell_width) row = round((y - self.__margin * 2) / self.__cell_width) stage_row = (y - self.__margin) - (self.__cell_width * row + self.__margin) stage_col = (x - self.__margin) - (self.__cell_width * col + self.__margin) if stage_col < stage_row: self.direct = 1 else: self.direct= 0 if self.valide(row, col, self.direct): if self.__history % 4 == 0 or (self.__history + 2) % 4 == 0: self.__game_board.drew_turn(2) else: self.__game_board.drew_turn(1) self.add_logic(row, col, self.color) self.__game_board.draw_chess(row, col, self.color, self.direct) if self.judge_owner(row, col, self.color, self.direct): self.__game_board.drew_turn(self.judge_next(self.color)) for i in self.judge_owner(row, col, self.color, self.direct): x,y=self.draw_owner(i) self.__game_board.drew_owner(self.color, y, x) else: self.__game_board.drew_turn(self.color) self.judge_color(row, col, self.color, self.direct) print(self.logic_board_state) if 0 not in self.logic_board_owner: self.__game_board.pop_win(self.judge_winner())
07-14
这段代码是一个名为 `__next_step` 的方法。它接收两个参数 `x` 和 `y`,代表鼠标点击的坐标位置。 首先,根据 `self.judge_colory` 的值来判断是否需要更新 `self.__history`。如果 `self.judge_colory` 为假,则 `self.__history` 不变,否则将 `self.__history` 加 1。 接下来,根据 `self.__history` 的奇偶性来确定当前的颜色。如果 `self.__history` 是偶数,则 `self.color` 设置为 1,否则设置为 2。 如果 `self.start_ai_game` 为真,则进入 AI 对战模式。根据当前的颜色和 AI 的颜色判断是否轮到 AI 下棋。如果是,则调用 `self.ai_stage` 方法,传入当前棋盘状态和当前颜色,获取 AI 下棋的结果,并将结果赋值给 `row` 和 `col`。 如果不是 AI 下棋,即玩家下棋,则将鼠标点击位置转换为行和列的索引,并计算出相对于棋盘格子的位置。根据相对位置的大小,确定下棋方向,并将结果赋值给 `self.direct`。 接下来,通过调用 `self.valide` 方法判断当前位置是否可下棋。如果可下棋,则根据当前回合数判断应该绘制哪种颜色的标记,并调用相应的方法在游戏界面上绘制标记和棋子。 然后,通过调用 `self.judge_owner` 方法判断是否有棋子归属变更,并返回变更的位置。如果有变更,根据变更的位置绘制相应颜色的棋子。 接下来,通过调用 `self.judge_color` 方法更新逻辑棋盘的状态。 然后,打印出当前逻辑棋盘的状态。 最后,判断逻辑棋盘是否已满。如果已满,则调用 `self.judge_winner` 方法判断胜利方,并在游戏界面上弹出胜利提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值