Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / LetterCombinationsofaPhoneNumber.java
Last active March 16, 2016 14:03
LeetCode - Letter Combinations of a Phone Number
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/
// Solution 1 : Iterative
public List<String> letterCombinations(String digits) {
String[] maps = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ArrayList<String> ret = new ArrayList<>();
if(digits == null || digits.length() == 0){
return ret;
}
ret.add("");
@cangoal
cangoal / GenerateParentheses.java
Last active March 17, 2016 17:49
LeetCode - Generate Parentheses
// Using String
public List<String> generateParenthesis(int n) {
ArrayList<String> ret = new ArrayList<String>();
if(n<1) return ret;
String str = "";
helper(n,n,ret, str);
return ret;
}
private void helper(int left, int right, ArrayList<String> ret, String str){
@cangoal
cangoal / Remove Duplicates from Sorted Array
Last active August 29, 2015 14:22
LeetCode - Remove Duplicates from Sorted Array
//
public int removeDuplicates(int[] nums) {
if(nums == null) return 0;
if(nums.length < 2) return nums.length;
int k = 0, p = 1;
while(p < nums.length){
if(nums[k] != nums[p]){
nums[++k] = nums[p];
}
p++;
@cangoal
cangoal / Remove Element
Created June 2, 2015 19:10
LeetCode - Remove Element
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length == 0) return 0;
int k = 0;
for(int i=0; i<nums.length; i++){
if(nums[i] != val){
nums[k++] = nums[i];
}
}
return k;
}
@cangoal
cangoal / Search for a Range
Created June 2, 2015 20:23
LeetCode - Search for a Range
// Solution 1: 3 binary searches
public int[] searchRange(int[] nums, int target) {
int[] ret = new int[]{-1, -1};
if(nums == null || nums.length == 0) return ret;
int left = 0, right = nums.length - 1;
int mid = (left + right) / 2;
while(left < right){
if(nums[mid] == target){
ret[0] = mid;
ret[1] = mid;
@cangoal
cangoal / JumpGame.java
Last active March 28, 2016 05:36
LeetCode - Jump Game
//
public boolean canJump(int[] nums) {
if(nums.length == 0) return false;
int i=-1, reach = 0;
while(i < reach){
if(reach >= nums.length-1) return true;
i++;
reach = Math.max(reach, i+nums[i]);
}
return false;
@cangoal
cangoal / JumpGameII.java
Last active March 14, 2016 19:09
LeetCode - Jump Game II
public int jump(int[] A) {
// write your code here
int reach = 0, step = 0, max = 0;
for(int i=0; i<=reach; i++){
if(reach >= A.length-1) return step;
max = Math.max(max, A[i]+i);
if(i == reach){
reach = max;
step++;
}
@cangoal
cangoal / First Missing Positive
Last active August 29, 2015 14:22
LeetCode - First Missing Positive
public int firstMissingPositive(int[] nums) {
if(nums == null || nums.length==0) return 1;
for(int i=0; i<nums.length; i++){
while(nums[i]>0 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1]){
int temp = nums[nums[i] -1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
@cangoal
cangoal / Permutations.java
Last active March 16, 2016 04:26
LeetCode - Permutations
// Solution 1 : recursive
public ArrayList<ArrayList<Integer>> permute(int[] num) {
if(num==null || num.length==0) return null;
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
helper(num, new boolean[num.length], new ArrayList<Integer>(), ret);
return ret;
}
private void helper(int[] num, boolean[] used, ArrayList<Integer> temp, ArrayList<ArrayList<Integer>> ret){
@cangoal
cangoal / PermutationsII.java
Last active March 16, 2016 04:27
LeetCode - Permutations II
// Original solution -- recursive
public ArrayList<ArrayList<Integer>> permuteUnique(int[] nums) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
if(nums == null || nums.length == 0) return ret;
Arrays.sort(nums);
boolean[] flags = new boolean[nums.length];
helper(ret, flags, nums, new ArrayList<Integer>(), 0);
return ret;
}