首页 > 代码库 > [LintCode] Kth Prime Number
[LintCode] Kth Prime Number
http://lintcode.com/en/problem/kth-prime-number/#
class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */ public long kthPrimeNumber(int k) { // write your code here Queue<Long> q3 = new LinkedList<>(); q3.offer(3L); Queue<Long> q5 = new LinkedList<>(); q5.offer(5L); Queue<Long> q7 = new LinkedList<>(); q7.offer(7L); long v = 0; for (int i = 0 ; i < k ; i ++) { // Find min element from 3 tops. if (q3.peek() < q5.peek() && q3.peek() < q7.peek()) { v = q3.poll(); q3.offer(v * 3); q5.offer(v * 5); q7.offer(v * 7); } else if (q5.peek() < q3.peek() && q5.peek() < q7.peek()) { v = q5.poll(); q5.offer(v * 5); q7.offer(v * 7); } else { v = q7.poll(); q7.offer(v * 7); } } return v; } };
[LintCode] Kth Prime Number
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。