1. 程式人生 > >【LeetCode】數學(共106題)

【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