Wednesday, December 14, 2016

常见数学的套路

1. 问factor:
先mod查 num %n ==0, 然后除 recur(num/n)

Ex: Ugly Number

https://leetcode.com/problems/ugly-number/

2查几个prime number
 因为prime 只能被 1和自己整除,如果是其他2*什么的就不行

这里不用O(N^2)每次查当前数是不是prime,可以用boolean array,
把已经判断过的结果存下来,节约时间,略tricky(注意从2开始)
https://leetcode.com/problems/count-primes/


3.数学前后运算步骤
https://leetcode.com/problems/excel-sheet-column-title/

n--;//for A                                                      res *=res;
sb.insert(0, (char)('A' + n % 26));                    res = res + s.charAt(i)-'A'+1;
n /= 26;

No comments:

Post a Comment