井字游戏
思路:
朴素求解
- 先判断横、竖、两个对角线
- 如果前面判断无法得出结果,用函数
countSpace
判断是否有空格,有代表没有填满,输出Pending
、否则输出Draw
。代码:
/** * @param {string[]} board * @return {string} */ var tictactoe = function (board) { if (board.length == 1 || !board[0][0]) { return board[0][0]; } if (horizontal(board) || vertical(board) || diagonal(board) || negativeDiagonal(board)) { return horizontal(board) || vertical(board) || diagonal(board) || negativeDiagonal(board) } return countSpace(board) ? "Pending" : "Draw"; }; function horizontal(board) { for (let i = 0; i < board.length; i++) { let count = 1; let char = board[i][0]; for (let j = 1; j < board[i].length; j++) { if (char == ' ' || char !== board[i][j]) { break; } else { count++; } } if (count === board.length) { return char; } } } function vertical(board) { for (let i = 0; i < board.length; i++) { let count = 1; let char = board[0][i]; for (let j = 1; j < board[i].length; j++) { if (char == ' ' || char !== board[j][i]) { break; } else { count++; } } if (count === board.length) { return char; } } } function diagonal(board) { let count = 1; let char = board[0][0]; for (let i = 1; i < board.length; i++) { if (char == ' ' || char !== board[i][i]) { break; } else { count++; } } if (count === board.length) { return char; } } function negativeDiagonal(board) { let count = 1; let j = board.length - 1; let char = board[0][j]; for (let i = 1; i < board.length; i++) { if (char == ' ' || char !== board[i][j - 1]) { break; } else { count++; } j--; } if (count === board.length) { return char; } } function countSpace(board) { for (let i = 0; i < board.length; i++) { for (let j = 0; j < board[i].length; j++) { if (board[i][j] == ' ') { return true; } } } return false; }
阶乘尾零
标签:count,char,20,金典,---,length,let,board,return From: https://www.cnblogs.com/dgqp/p/17369784.html代码:
/** * @param {number} n * @return {number} */ // 除5取整的问题 var trailingZeroes = function(n) { let res = 0 while(n !== 0){ n = Math.floor(n / 5) res += n } return res };