• 1275. Find Winner on a Tic Tac Toe Game


    package LeetCode_1275
    
    /**
     * 1275. Find Winner on a Tic Tac Toe Game
     * https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/description/
     *
     * Example 1:
    Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
    Output: "A"
    Explanation: "A" wins, he always plays first.
    "X  "    "X  "    "X  "    "X  "    "X  "
    "   " -> "   " -> " X " -> " X " -> " X "
    "   "    "O  "    "O  "    "OO "    "OOX"
    
    Constraints:
    1 <= moves.length <= 9
    moves[i].length == 2
    0 <= moves[i][j] <= 2
    There are no repeated elements on moves.
    moves follow the rules of tic tac toe.
     * */
    class Solution {
        fun tictactoe(moves: Array<IntArray>): String {
            val n = 3
            val rows = IntArray(n)
            val cols = IntArray(n)
            var dia1 = 0
            var dia2 = 0
            //A is 1
            var player = 1
    
            for (move in moves) {
                val x = move[0]
                val y = move[1]
                rows[x] += player
                cols[y] += player
                //diagonal from top left to right bottom
                if (x == y) {
                    dia1 += player
                }
                //diagonal from top right to left bottom
                if (x == n - y - 1) {
                    dia2 += player
                }
                //change player every turn
                player *= -1
            }
    
            for (i in 0 until n) {
                if (rows[i] == n || cols[i] == n || dia1 == n || dia2 == n) {
                    return "A"
                }
                if (rows[i] == -n || cols[i] == -n || dia1 == -n || dia2 == -n) {
                    return "B"
                }
            }
    
            if (moves.size < n * n) {
                return "Pending"
            } else {
                return "Drawing"
            }
        }
    }
  • 相关阅读:
    vue中封装公共方法,全局使用
    element-ui table 最后一行合计,单元格合并
    vuex 进行封装
    vue生命周期
    (转)no terminal library found
    解压
    (转)bash: make: command not found
    (转)linux 批量删除文件命令
    python
    Session
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13182544.html
Copyright © 2020-2023  润新知