由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个简单的金融公司的coding面试题
相关主题
saleforce 店面,攒人品吧。问道题: prime factor
请教一道Amazon面世题Two Sigma 面试
请问个算法复杂度Cracking the Coding Interview fifth edition
Amazon电话面试第一轮I have one program to find primer between 2 and 1023 with bitset, but I don't understand one line
请教:find Kth prime number苦等了三个星期,终于等到offer来啦!(完)
leetcode能不能多加点DP的题啊问一个面试问题
google第二次店面,也太简单了,就是哥德巴赫猜想也问一个median的问题
square的phone coding screen都会问什么问题?flextrade面经
相关话题的讨论汇总
话题: return话题: int话题: sqrt话题: isprime话题: boolean
进入JobHunting版参与讨论
1 (共1页)
w*****1
发帖数: 245
1
给个整数, return是不是prime: boolean isPrime(int n);
我知道的解法: <=2的数单独处理, 然后for(int i=3; i if(n % i == 0)
return false;
}
return true;
有没有更优化的做法??
f*********5
发帖数: 576
2
what will u return if n=16?

【在 w*****1 的大作中提到】
: 给个整数, return是不是prime: boolean isPrime(int n);
: 我知道的解法: <=2的数单独处理, 然后for(int i=3; i: if(n % i == 0)
: return false;
: }
: return true;
: 有没有更优化的做法??

s*********t
发帖数: 1663
3
他说小于等于2单独处理

【在 f*********5 的大作中提到】
: what will u return if n=16?
I**A
发帖数: 2345
4
感觉速度上已经很优化了吧?
不过,code有问题
1)n<9, all are true
2)为嘛要i increase by 2, if n=16, it will return true as well

【在 w*****1 的大作中提到】
: 给个整数, return是不是prime: boolean isPrime(int n);
: 我知道的解法: <=2的数单独处理, 然后for(int i=3; i: if(n % i == 0)
: return false;
: }
: return true;
: 有没有更优化的做法??

f*********5
发帖数: 576
5
16属于那个系列的?
我认为他说的是n==1,n==2 直接return

【在 s*********t 的大作中提到】
: 他说小于等于2单独处理
f*********5
发帖数: 576
6
for的3项内容,我认为都有问题
for(int i=2; i<=sqrt(n); i++)
这样才对吧?

【在 w*****1 的大作中提到】
: 给个整数, return是不是prime: boolean isPrime(int n);
: 我知道的解法: <=2的数单独处理, 然后for(int i=3; i: if(n % i == 0)
: return false;
: }
: return true;
: 有没有更优化的做法??

s*********t
发帖数: 1663
7
RE
我居然连这么弱智的错误都没发现。。
彻底受打击了

【在 f*********5 的大作中提到】
: for的3项内容,我认为都有问题
: for(int i=2; i<=sqrt(n); i++)
: 这样才对吧?

I**A
发帖数: 2345
8
那,到底有没有更优化的办法?

【在 f*********5 的大作中提到】
: for的3项内容,我认为都有问题
: for(int i=2; i<=sqrt(n); i++)
: 这样才对吧?

h**6
发帖数: 4160
9
sieve可以一次性找出不超过n的所有质数,不过如果只判断一个数是不是质数,就用楼
主的办法再加个判断奇偶就可以了。
t***s
发帖数: 602
10
伊好像搞混了i和n..

【在 f*********5 的大作中提到】
: for的3项内容,我认为都有问题
: for(int i=2; i<=sqrt(n); i++)
: 这样才对吧?

相关主题
leetcode能不能多加点DP的题啊问道题: prime factor
google第二次店面,也太简单了,就是哥德巴赫猜想Two Sigma 面试
square的phone coding screen都会问什么问题?Cracking the Coding Interview fifth edition
进入JobHunting版参与讨论
f*********5
发帖数: 576
11
有点那个意思
貌似伊每次+2是想跳过偶数,其实这个idea不错的

【在 t***s 的大作中提到】
: 伊好像搞混了i和n..
a****j
发帖数: 8
12
if ((n & 1) == 0) {
return false;
}
const int m = sqrt(n);
for (int i=3; i<=m; i+=2) {
}

【在 f*********5 的大作中提到】
: for的3项内容,我认为都有问题
: for(int i=2; i<=sqrt(n); i++)
: 这样才对吧?

w*****1
发帖数: 245
13
sorry我for中间项的确漏了=.
没有更好方法了吗??
w*****1
发帖数: 245
14
what about generate prime numbers until n?
void printPrimes(int n);
查了半天网上好像都是Sieve of Eratosthenes, 一个老算法.
I**A
发帖数: 2345
15
上次amazon phone刚刚让我code了这个
我就用了这个Sieve of Eratosthenes老方法
你们谁给看看这个复杂度怎么算
O(n*(sqrt(2)+sqrt(3)+sqrt(4)+...sqrt(n))
wiki上说是O(n(logn)(loglogn)),怎么算的?
唉,上次给了人家一个O(n), 后来我发email更正了说不是O(n),可是也没给我个回音儿
---------------
//find all the prime numbers between 2 and n(inclusive)
public void findPrimes(int n){
if(n<2)
return;
boolean[] isPrime = new boolean[n+1];

//initialize the boolean array to true;
for(int i=2; i<=n; i++)
isPrime[i] = tru

【在 w*****1 的大作中提到】
: what about generate prime numbers until n?
: void printPrimes(int n);
: 查了半天网上好像都是Sieve of Eratosthenes, 一个老算法.

v******a
发帖数: 54
16

音儿
increase

【在 I**A 的大作中提到】
: 上次amazon phone刚刚让我code了这个
: 我就用了这个Sieve of Eratosthenes老方法
: 你们谁给看看这个复杂度怎么算
: O(n*(sqrt(2)+sqrt(3)+sqrt(4)+...sqrt(n))
: wiki上说是O(n(logn)(loglogn)),怎么算的?
: 唉,上次给了人家一个O(n), 后来我发email更正了说不是O(n),可是也没给我个回音儿
: ---------------
: //find all the prime numbers between 2 and n(inclusive)
: public void findPrimes(int n){
: if(n<2)

1 (共1页)
进入JobHunting版参与讨论
相关主题
flextrade面经请教:find Kth prime number
一个查找算法题leetcode能不能多加点DP的题啊
一道算法题目google第二次店面,也太简单了,就是哥德巴赫猜想
c++ questionsquare的phone coding screen都会问什么问题?
saleforce 店面,攒人品吧。问道题: prime factor
请教一道Amazon面世题Two Sigma 面试
请问个算法复杂度Cracking the Coding Interview fifth edition
Amazon电话面试第一轮I have one program to find primer between 2 and 1023 with bitset, but I don't understand one line
相关话题的讨论汇总
话题: return话题: int话题: sqrt话题: isprime话题: boolean