math
yuanhisn
这个作者很懒,什么都没留下…
展开
-
判断一个点是否在三角形内
给定平面上一点p(x0,y0),判断该点是否在三角形ABC中,三角形顶点坐标分别为A(xa,xb),B(xb,yb),C(xc,yc)。可以使用面积法来判断,方法如下:其中S(A,B,C)表示三角形ABC的面积。 若abs( S(A,B,C) ) = abs( S(P,B,C) ) + abs( S(A,P,C) ) + abs( S(A,B,P) ) ,则P在三角形ABC的内部或边...原创 2015-04-10 05:51:34 · 143 阅读 · 0 评论 -
LeetCode 204 - Count Primes
Description: Count the number of prime numbers less than a non-negative number, n References: How Many Primes Are There? Sieve of Eratosthenes public int countPrimes(int n) { if(n < 2)...原创 2015-05-01 02:05:21 · 74 阅读 · 0 评论 -
LeetCode 12 - Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); ...原创 2015-06-10 14:30:12 · 62 阅读 · 0 评论 -
LeetCode 13 - Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. int romanToInt(string s) { unordered_map<char,int> map = {{'I',1},{'V',5},{...原创 2015-06-10 14:46:05 · 52 阅读 · 0 评论 -
Google Interview - 判断点是否在凸多边形内的O(logn)解法
补充知识:向量叉积,向量P = (x1, y1); Q = (x2, y2); P×Q = (x1*y2 - x2*y1);叉积的一个非常重要性质是可以通过它的符号判断两矢量相互之间的顺逆时针关系:若 P × Q > 0 , 则P在Q的顺时针方向。若 P × Q < 0 , 则P在Q的逆时针方向。若 P × Q = 0 , 则P与Q共线,但可能同向也可能反向。叉积的方向与进行叉积的两...原创 2015-06-25 15:20:06 · 88 阅读 · 0 评论 -
LeetCode 233 - Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following num...原创 2015-07-15 11:42:29 · 54 阅读 · 0 评论 -
10001st prime number
Question: https://projecteuler.net/problem=7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? 思路可以参考 wiki...原创 2015-01-06 12:03:42 · 199 阅读 · 0 评论 -
LeetCode 166 - 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. F...原创 2015-01-15 13:49:18 · 54 阅读 · 0 评论 -
Reservoir Sampling - Choose K Numbers with Uniform Probability
Given that you receive one number at a time(streaming numbers), choose K numbers with uniform probability. 题目要求从n个input中random sample k个数 (k<=n)其中n是未知的,input是以online的形式获得的。即给定一个function getInput...原创 2015-01-31 03:51:52 · 85 阅读 · 0 评论 -
Google Interview - 数字旋转180度
找出1~10^n中数字翻转过来是本身的数( 96 ->96, 18 -> 81, 0 -> 0, 其他数字翻过来都不是数字) 设计一个函数,判断某个只包含数字的字符串是不是Ambigram(比如,69,88,609,818等都是Ambigram)。Follow up:给一个整数n,找出所有长为n的Ambigram。 可以先求出n-2的答案,再利用n-2的答案求出n的答案,...原创 2015-03-20 05:46:35 · 98 阅读 · 0 评论