Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / Majority Element
Created June 8, 2015 17:45
LeetCode - Majority Element
//
public int majorityElement(int[] nums) {
if(nums == null || nums.length == 0) return -1;
int ret = nums[0], count=1;
for(int i=1; i<nums.length; i++){
if(count == 0){
ret = nums[i];
count = 1;
}
else if(ret == nums[i]){
@cangoal
cangoal / FactorialTrailingZeroes.java
Last active March 25, 2016 14:44
LeetCode - Factorial Trailing Zeroes
//
public int trailingZeroes(int n) {
int ret = 0;
while(n != 0){
ret += n/5;
n = n/5;
}
return ret;
}
//
@cangoal
cangoal / Largest Number
Created June 8, 2015 18:34
LeetCode - Largest Number
//
public String largestNumber(int[] nums) {
if(nums == null || nums.length==0) return null;
String str = "";
ArrayList<Integer> list = new ArrayList<>();
for(int num : nums){
list.add(num);
}
Collections.sort(list, comparator);
for(int i=0; i<list.size(); i++){
@cangoal
cangoal / Reverse Bits
Last active January 13, 2016 18:56
LeetCode - Reverse Bits
// better solution
public int reverseBits(int n) {
int ret = 0;
for(int i=0; i<32; i++){
ret = (ret << 1) + (n & 1);
n >>=1;
}
return ret;
}
@cangoal
cangoal / Number of 1 Bits
Last active August 29, 2015 14:22
LeetCode - Number of 1 Bits
public int hammingWeight(int n) {
int ret = 0;
while(n != 0){
n &= n-1;
ret++;
}
return ret;
}
@cangoal
cangoal / Binary Tree Right Side View
Last active August 29, 2015 14:22
LeetCode - Binary Tree Right Side View
//
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
helper(ret, root, 0);
return ret;
}
private void helper(List<Integer> ret, TreeNode root, int level){
if(root == null) return;
if(ret.isEmpty() || level >= ret.size()){
@cangoal
cangoal / Number of Islands
Last active August 29, 2015 14:22
LeetCode - Number of Islands
// BFS ---> note: this my cause issue when you encode the coordinates
public int numIslands(char[][] grid) {
if(grid==null || grid.length==0) return 0;
int count = 0;
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[0].length; j++){
if(grid[i][j] == '1'){
count++;
setZeros(grid, i, j);
}
@cangoal
cangoal / Bitwise AND of Numbers Range
Last active August 29, 2015 14:22
LeetCode - Bitwise AND of Numbers Range
//
public int rangeBitwiseAnd(int m, int n) {
while(m<n) n = n & (n-1);
return n;
}
//
public int rangeBitwiseAnd(int m, int n) {
return (n > m) ? (rangeBitwiseAnd(m/2, n/2) << 1) : m;
}
@cangoal
cangoal / HappyNumber.java
Last active March 16, 2016 14:09
LeetCode - Happy Number
// O(1) space complexity
int digitSquareSum(int n) {
int sum = 0, tmp;
while (n) {
tmp = n % 10;
sum += tmp * tmp;
n /= 10;
}
return sum;
}
@cangoal
cangoal / Remove Linked List Elements
Created June 9, 2015 19:08
LeetCode - Remove Linked List Elements
public ListNode removeElements(ListNode head, int val) {
if(head == null) return head;
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode pre = dummyNode;
while(pre.next != null){
ListNode cur = pre.next;
if(cur.val == val){
pre.next = pre.next.next;