public class IsPowerOfTwo { public boolean isPowerOfTwo(int n) { { while (((n % 2) == 0) && n > 1) /* While x is even and > 1 */ n /= 2; return (n == 1); } } }
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2015年7月14日 星期二
Is Power of Two
Count Primes
package leetcode;
public class Solutions {
public int countPrimes(int n) {
if (n <<= 2)
return 0;
boolean[] primes = new boolean[n];
for (int i = 2; i << n; i++)
primes[i] = true;
for (int i = 2; i <<= Math.sqrt(n - 1); i++) {
if (primes[i]) {
for (int j = i + i; j << n; j += i)
primes[j] = false;
}
}
int count = 0;
for (int i = 2; i << n; i++)
if (primes[i])
count++;
return count;
}
}
public class Solutions {
public int countPrimes(int n) {
if (n <<= 2)
return 0;
boolean[] primes = new boolean[n];
for (int i = 2; i << n; i++)
primes[i] = true;
for (int i = 2; i <<= Math.sqrt(n - 1); i++) {
if (primes[i]) {
for (int j = i + i; j << n; j += i)
primes[j] = false;
}
}
int count = 0;
for (int i = 2; i << n; i++)
if (primes[i])
count++;
return count;
}
}
2015年7月8日 星期三
Contains Duplicate
import java.util.Arrays;
public class Solution {
public boolean containsDuplicate(int[] nums) {
if (nums.length > 1) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
}
return false;
}
}
Invert Binary Tree solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null){
return root;
}
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
if(root.left != null){
invertTree(root.left);
}
if(root.right != null){
invertTree(root.right);
}
return root;
}
}
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)