Leetcode#380. Insert Delete GetRandom O(1)
ProblemImplement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). ...
Leetcode#189. Rotate Arra
ProblemGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
1234567Input: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
123456Input: nums = [-1,-100,3,99], k = 2Output: [3,99,-1,-100]Explanation:rotate 1 steps to the right: [99,-1,-100,3]rotate 2 steps to the right: [3,99,-1,-100]
...
Leetcode#202. Happy Number
ProblemWrite an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
12345678Input: n = 19Output: ...
Leetcode#172. Factorial Trailing Zeroes
ProblemGiven an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Example 1:
1234Input: n = 3Output: 0Explanation: 3! = 6, no trailing zero.
Example 2:
1234Input: n = 5Output: 1Explanation: 5! = 120, one trailing zero.
Example 3:
123Input: n = 0Output: 0
Constraints:
0 <= n <= 10^4
Follow up: Could you write a solution that works in logarithmic time complexity?
Solve這題先搞懂 trailing zeroes,是指尾巴的0
要計算尾巴有幾個0很簡單就是算其中乘了幾次(5*2)
由於每 ...
Leetcode#224. Basic Calculator
ProblemGiven a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
123Input: s = "1 + 1"Output: 2
Example 2:
123Input: s = " 2-1 + 2 "Output: 3
Example 3:
123Input: s = "(1+(4+5+2)-3)+(6+8)"Output: 23
Constraints:
1 <= s.length <= 3 * 10^5
s consist ...
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#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: ...
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 ...
Leetcode#70. Climbing Stairs
ProblemYou are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
123456Input: n = 2Output: 2Explanation: There are two ways to climb to the top.1. 1 step + 1 step2. 2 steps
Example 2:
1234567Input: n = 3Output: 3Explanation: There are three ways to climb to the top.1. 1 step + 1 step + 1 step2. 1 step + 2 steps3. 2 steps + 1 step
Constraints:
1 <= n <= 45
Solve用一般的**fibona ...