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" ...
Leetcode#212. Word Search II
簡易leetcode79
ProblemGiven an m x n board of characters and a list of strings words, return all words on the board.
Each word must 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 in a word.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/07/search1.jpg
123Input: board = [["o","a","a","n"],["e","t",&quo ...
Leetcode#35. Search Insert Position
ProblemGiven a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
123Input: nums = [1,3,5,6], target = 5Output: 2
Example 2:
123Input: nums = [1,3,5,6], target = 2Output: 1
Example 3:
123Input: nums = [1,3,5,6], target = 7Output: 4
Constraints:
1 <= nums.length <= 10^4
10^4 <= nums[i] <= 10^4
...
Leetcode#62. Unique Paths
123456789101112- [Leetcode]- [Python]- [medium]- [💡]- Math- Dynamic Programming- Combinatoricscover: /img/cover/leetcode.jpgcategories: Leetcode
ProblemThere is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot c ...