You are playing the following Nim Game with your friend:
Initially, there is a heap of stones on the table.
You and your friend will alternate taking turns, and you go first.
On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
The one who removes the last stone is the winner.
Given n, the number of stones in the heap, return true if you can win the game a......
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:
......