Leetcode#52. N-Queens II
ProblemThe n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example 1:
![(https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
!https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
1234Input: n = 4Output: 2Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
123Input: n = 1Output: 1
Constraints:
...
Leetcode#530. Minimum Absolute Difference in BST
ProblemGiven the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
123Input: root = [4,2,6,1,3]Output: 1
Example 2:
!https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
123Input: root = [1,0,48,null,null,12,49]Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 10^4].
0 <= Node.val <= 10^5
Solve法一由於有有序性質、且正整 ...
我的 hexo md排版
我是使用notion 做一做 再複製貼上到 ,hexo生成的md上
例如:leetcode149這篇
基本problem是放 leetcode那邊複製過來的,但圖片由於從notion上複製時,無法讀取,所以就自己加了
1
的連結
程式碼在notion 也有 ,都是在notionu編輯完直接全部複製
這篇在notion長這樣
這篇圖片問題就是,複製時這圖片並非網址,notion也未公開,就不修正了
但我依舊偷渡notion的圖片利用 view original link
Leetcode#9. Palindrome Number
ProblemGiven an integer x, return true if x is a
palindrome
, and false otherwise
1234PalindromeAn integer is a palindrome when it reads the same forward and backward.For example, 121 is a palindrome while 123 is not
.
Example 1:
1234Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.
Example 2:
1234Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Ex ...
Leetcode#149. Max Points on a Line
ProblemGiven an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg
123Input: points = [[1,1],[2,2],[3,3]]Output: 3
Example 2:
!https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg
123Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]Output: 4
Constraints:
1 <= points.length <= 300
points[i].length == 2
10^4 & ...
Leetcode#221. Maximal Square
ProblemGiven an m x n binary matrix filled with 0‘s and 1‘s, find the largest square containing only 1‘s and return its area.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg
123Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0& ...
Leetcode#190. Reverse Bits
ProblemReverse bits of a given 32 bits unsigned integer.
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the sig ...
Leetcode#67. Add Binary
ProblemGiven two binary strings a and b, return their sum as a binary string.
Example 1:
123Input: a = "11", b = "1"Output: "100"
Example 2:
123Input: a = "1010", b = "1011"Output: "10101"
Constraints:
1 <= a.length, b.length <= 10^4
a and b consist only of '0' or '1' characters.
Each string does not contain leading zeros except for the zero itself.
Solve123456789101112131415161718192021222324class Solution: ...
How to create a trie in Python
How to create a trie in PythonTrieTrie 像一顆特別的樹,每個節點都是一個字母,從根節點到葉節點的路徑上的字母連起來就是一個單詞。
用途搜尋提示,比如輸入一個單詞,自動提示可能的後續單詞。
優點搜尋效率高,不用像哈希表一樣,需要計算哈希值,直接根據單詞的每個字母,一層一層的往下搜尋即可。
缺點空間消耗大,因為每個節點都需要存儲子節點的指針,當單詞數量很多時,需要的空間就很大。
建立 Trie1234567891011121314151617181920212223242526272829303132class TrieNode: def __init__(self): self.children = {} self.is_word = Falseclass Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root f ...
Leetcode#79. Word Search
進階leetcode212
ProblemGiven an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/04/word2.jpg
123Input: board = [["A","B","C","E"],["S","F","C" ...