Given an integer n, return the number of prime numbers that are strictly less than n.
https://leetcode.com/problems/count-primes/
Solution
Code
python
class Solution:
def countPrimes(self, n: int) -> int:
if n <= 1:
return 0
tmp = [True] * n
for i in range(2, n):
if(tmp[i]):
for j in range(i*i, n, ......
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
https://leetcode.com/problems/factorial-trailing-zeroes/
Solution
Count How many 5 in the n
Code
python
class Solution:
def trailingZeroes(self, n: int) -> int:
ret = 0
while n >= 5:
n //= 5
ret += n
return ret
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
https://leetcode.com/problems/missing-number/
code
python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
# 应该的总和 - 实际的总和
return (len(nums) * (len(nums) + 1)) // 2 - sum(nums) # 利用......
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
https://leetco......
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
https://leetcode.com/problems/fibonacci-number/
Solution
Violent Hacking / DP Table
Code
Violent Hacking
O(2^n)
submit code
class Solution:
def fib(self, n: int) -> int:
......
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
https://leetcode.com/problems/move-zeroes/
Solution
Double Pointer / Fast And Slow Pointer
Same with https://www.emperinter.info/2022/03/15/remove-duplicates-from-sorted-list/
we need......