Friday, August 28, 2015

Happy Number

Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1
Solution: 线性的复杂度O(n)
public class Solution {
    public boolean isHappy(int n) {
        HashSet set = new HashSet<>();
        
        while (!set.contains(n)) {
            set.add(n);
            n = sum(getDigit(n));
            if (n == 1) {
                return true;
            }
        }
        return false;
    }
    
    public int sum(int[] arr) {
        int sum = 0;
        for(int i : arr) {
            sum += i * i;
        }
        return sum;
    }
    
    public int[] getDigit(int n) {
        String s = String.valueOf(n);
        
        int[] arr = new int[s.length()];
        int i = 0;
        while (n >= 1) {
            int m = n % 10;
            arr[i] = m;
            i++;
            n = n / 10;
        }
        return arr;
    }
}

Wednesday, August 26, 2015

Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
Solution: Run time complexity is O(n), space complexity is O(n).
public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap map = new HashMap<>();
        
        for(int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                int pre = map.get(nums[i]);
                if (i - pre <= k) {
                    return true;
                }
            }
            map.put(nums[i], i);
        }
        
        return false;
    }
}

Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Solution1: Run time complexity of Arrays.sort is O(nlogn), so total run time complexity is O(nlogn), constant space. 
public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return false;
        }
        
        Arrays.sort(nums);
        
        int i = 0;
        int j = i + 1;
        
        while (j < nums.length) {
            if (nums[i] == nums[j]) {
                return true;
            } else {
                i++;
                j++;
            }
        }
        return false;
    }
}
Solution2: Run time complexity is O(n), space complexity is O(n).
public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return false;
        }
        
        HashSet set = new HashSet();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return true;
            } else {
                set.add(nums[i]);
            }
        }
        return false;
    }
}
Or
public class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        
        HashSet set = new HashSet();
        for (int i : nums) {
            if (!set.add(i)) {
                return true;
            }
        }
        return false;
    }
}