本文主要是对leetcode中的math类算法的总结,包含了各题的solution和常用的解决方法。
相关源码:code
204. Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解题思路:本题利用了素数的概念和打表法 ,代码如下:
1 | class Solution { |
263. Ugly Number
Description:Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5
. For example, 6, 8
are ugly while 14
is not ugly since it includes another prime factor 7
.
Note that 1
is typically treated as an ugly number.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
解题思路:直接除以2,3,5,代码如下:
1 | class Solution { |
400. Nth Digit
Description:Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
1 | Input: |
Example 2:
1 | Input: |
代码如下:
1 | class Solution { |
633. Sum of Square Numbers
Description:Given a non-negative integer c
, your task is to decide whether there’re two integers a
and b
such that a2 + b2 = c.
Example 1:
1 | Input: 5 |
Example 2:
1 | Input: 3 |
解题思路:使用双指针逼近,代码如下:
1 |
|
367. Valid Perfect Square
Description:
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt
.
Example 1:
1 | Input: 16 |
Example 2:
1 | Input: 14 |
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
代码如下:
1 | class Solution { |
172. Factorial Trailing Zeroes
Description:
Given an integer n, return the number of trailing zeroes in n!.
**Note: **Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
1 | class Solution { |