leetcode-math
proudmore
这个作者很懒,什么都没留下…
展开
-
Leetcode Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For exam原创 2015-04-30 10:11:21 · 128 阅读 · 0 评论 -
Leetcode Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.最后的解法就是对n/5+n/25+n/125+…+进行求和,当n小于分母的时候,停止。分母依次为5^1, 5^2, 5^2… 这样的话在计算5^2的时候,原创 2015-04-30 11:11:40 · 300 阅读 · 1 评论 -
Leetcode Count primes
Count the number of prime numbers less than a non-negative number, n很容易想到一个一个check的方法,注意只需要check到 sqrt(n). 下面是一个空间换时间的方法,每次选上次得到的最后一个prime,然后把乘积全部cross out. 下一个没有被cross out的一定是prime,因为所有比他小的数的乘积都不hit[原创 2015-05-20 05:14:23 · 164 阅读 · 0 评论 -
Leetcode Pow(x,n)
divide conquer. 注意正负,overflowpublic class Solution { public double myPow(double x, int n) { int sign= (n%2==0 || x>0) ? 1 :-1; x=Math.abs(x); if(x==0 ||x==1)return sign*x;原创 2015-06-22 07:52:37 · 171 阅读 · 0 评论 -
Leetcode Divide two integers
divisor增加的时候*2,减小/2. handle integer overflow常用trick: longpublic class Solution { public int divide(int a, int b) { if(a==Integer.MIN_VALUE && b==-1)return Integer.MAX_VALUE; long d原创 2015-06-22 08:24:51 · 192 阅读 · 0 评论 -
Leetcode sqrt(x)
考点在于binary search 结束条件和overflow. 用loop invariant来理解: 每次loop都保证 [low,high]左边是严格小的,右边是严格大的.public class Solution { public int mySqrt(int x) { int low=1, high=x; if(x==0 || x==1)retur原创 2015-06-22 07:34:34 · 272 阅读 · 0 评论 -
Leetcode Max Points on a line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.[方法1]/** * Definition for a point. * class Point { * int x; * int y; * Point() { x =原创 2015-06-03 14:13:15 · 201 阅读 · 0 评论 -
Leetcode Valid number
public class Solution { public boolean isNumber(String s) { int len = s.length(); int i = 0, e = len - 1; while (i <= e && Character.isWhitespace(s.charAt(i))) i++;原创 2015-06-12 07:47:11 · 153 阅读 · 0 评论