【LeetCode】数学(共106题)

【2】Add Two Numbers 

【7】Reverse Integer 

【8】String to Integer (atoi) 

【9】Palindrome Number 

【12】Integer to Roman 

【13】Roman to Integer 

【29】Divide Two Integers 

【43】Multiply Strings 

【50】Pow(x, n) 

【60】Permutation Sequence 

【65】Valid Number 

【66】Plus One 

【67】Add Binary 

【69】Sqrt(x) 

【149】Max Points on a Line 

【166】Fraction to Recurring Decimal 

【168】Excel Sheet Column Title 

【171】Excel Sheet Column Number 

【172】Factorial Trailing Zeroes 

【202】Happy Number 

【204】Count Primes 

【223】Rectangle Area 

【224】Basic Calculator 

【231】Power of Two 

【233】Number of Digit One 

【246】Strobogrammatic Number 

【247】Strobogrammatic Number II 

【248】Strobogrammatic Number III 

【258】Add Digits 

【263】Ugly Number 

【264】Ugly Number II 

【268】Missing Number 

【273】Integer to English Words 

【279】Perfect Squares 

【296】Best Meeting Point 

【313】Super Ugly Number 

【319】Bulb Switcher 

【326】Power of Three 

【335】Self Crossing 

【343】Integer Break 

【356】Line Reflection 

【357】Count Numbers with Unique Digits 

【360】Sort Transformed Array 

【365】Water and Jug Problem 

【367】Valid Perfect Square 

【368】Largest Divisible Subset 

【372】Super Pow 

【396】Rotate Function 

【397】Integer Replacement 

【400】Nth Digit 

【413】Arithmetic Slices 

【415】Add Strings 

【423】Reconstruct Original Digits from English 

【441】Arranging Coins 

【453】Minimum Moves to Equal Array Elements 

【462】Minimum Moves to Equal Array Elements II 

【469】Convex Polygon 

【478】Generate Random Point in a Circle 

【483】Smallest Good Base 

【507】Perfect Number 

【517】Super Washing Machines 

【523】Continuous Subarray Sum 

【535】Encode and Decode TinyURL 

【537】Complex Number Multiplication 

【553】Optimal Division 

【573】Squirrel Simulation 

【592】Fraction Addition and Subtraction 

【593】Valid Square 

【598】Range Addition II 

【625】Minimum Factorization 

【628】Maximum Product of Three Numbers 

【633】Sum of Square Numbers 

【634】Find the Derangement of An Array 

【640】Solve the Equation 

【645】Set Mismatch 

【651】4 Keys Keyboard 

【660】Remove 9 

【670】Maximum Swap 

【672】Bulb Switcher II 

【728】Self Dividing Numbers 

【753】Cracking the Safe 

【754】Reach a Number 

【775】Global and Local Inversions 

【780】Reaching Points 

【781】Rabbits in Forest 

【782】Transform to Chessboard 

【789】Escape The Ghosts 

【794】Valid Tic-Tac-Toe State  (Oct 15th, 2018 每日一题)

通过字符串数组给定一个Tic-Tac-Toe(三连棋游戏,两人轮流在九格方盘上画'X'或者'O',谁先把三个相同记号排成横线、直线、斜线,即是胜者)状态board。 返回True如果当且仅当这个状态是一个有效的状态。 board是3x3数组,包含字符" ", "X", "O"。" "字符代表空的格。 

Tic-Tac-Toe游戏规则:

    1. 玩家只能轮流在空格(" ")里面画字符。
    2. 第一个玩家总是画"X",第二个玩家总是画"O"。
    3. "X"和"O"只能画在空白的格里面,不能画在已经存在"O"和"X"的格里。
    4. 三个相同记号排成横线、直线、斜线,即游戏结束。
    5. 如果没有空的格,游戏也结束。
    6. 游戏结束不能再移动。 
Example 1:
Input: board = ["O  ", "   ", "   "]
Output: false
Explanation: The first player always plays "X".

Example 2:
Input: board = ["XOX", " X ", "   "]
Output: false
Explanation: Players take turns making moves.

Example 3:
Input: board = ["XXX", "   ", "OOO"]
Output: false

Example 4:
Input: board = ["XOX", "O O", "XOX"]
Output: true

题解:参考知乎:https://zhuanlan.zhihu.com/p/34216982

首先我们要先熟悉下Tic-Tac-Toe三连棋游戏规则,就是两人轮流在九格方盘上画'X'或者'O',谁先把三个相同记号排成横线、直线、斜线,即游戏结束。 
那么我们从游戏规则中来找处所有不合理的状态。 
根据规则1和2,假设X的数目为countX, O的数目为countO,那么我们可以得到countX==countO,或者countX - countO == 1。 
根据游戏结束后则不能再画O和X,那么当countX==count时,如果存在三个X排成横线、直线、斜线,那么即是不合理的,因为X先画,当三个X排成横线、直线、斜线时, 此时游戏结束,不能再画O,所以O的数目应该比X的数目少1。 
当countX - countO == 1时,如果存在三个O排成横线、直线、斜线,那么是不合理的,因为当三个O排成横线、直线、斜线时,游戏结束,不能再画X,所以此时X的数目应该和O的数目相等。 

 1 class Solution {
 2 public:
 3     bool validTicTacToe(vector<string>& board) {
 4         int cntX = 0, cntO = 0;
 5         
 6         //1. X的个数要么和O个数相同,要么X个数比O的个数多一个
 7         for (int i = 0; i < 3; ++i) {
 8             for (int j = 0; j < 3; ++j) {
 9                 board[i][j] == 'X' ? cntX++ : board[i][j] == 'O' ? cntO++ : 1;
10             }
11         }
12         if (cntX != cntO && cntX != cntO+1) {
13             return false;
14         }
15         
16         //2. 判断最后一个放棋的是X,还是O
17         if (cntX - cntO == 1) {
18             //X最后放的棋子,所以每行,每列,每个对角线O不能有三连。
19             if(check(board, 'O') == false) {
20                 return false;
21             }
22         } else if (cntX == cntO) {
23             //O最后放的棋子,所以每行,每列,每个对角线X不能有三连。
24             if(check(board, 'X') == false) {
25                 return false;
26             }
27         }
28         return true;
29     }
30     bool check(vector<string>& board, char c) {
31         for (int i = 0; i < 3; ++i) {
32             string target = string(3, c);
33             if (board[i] == target) { return false;} // rows
34             if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == c) {  return false; } // cols
35         }
36         if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == c) { return false; }
37         if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == c) { return false; }
38         return true;
39     }
40 };
View Code

【800】Similar RGB Color 

【805】Split Array With Same Average 

【810】Chalkboard XOR Game 

【812】Largest Triangle Area 

【829】Consecutive Numbers Sum 

【836】Rectangle Overlap 

【858】Mirror Reflection 

【866】Prime Palindrome 

【868】Binary Gap 

【869】Reordered Power of 2 

【877】Stone Game 

【878】Nth Magical Number 

【883】Projection Area of 3D Shapes 

【885】Spiral Matrix III 

【887】Super Egg Drop 

【891】Sum of Subsequence Widths 

【892】Surface Area of 3D Shapes 

【899】Orderly Queue 

猜你喜欢

转载自www.cnblogs.com/zhangwanying/p/9790007.html
今日推荐