Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / Merge Intervals
Last active August 29, 2015 14:22
LeetCode - Merge Intervals
//
public List<Interval> merge(List<Interval> intervals) {
ArrayList<Interval> ret = new ArrayList<Interval>();
if(intervals == null || intervals.size() ==0) return ret;
Collections.sort(intervals, comparator);
Interval newInterval = intervals.get(0);
for(int i=1; i<intervals.size(); i++){
Interval curInterval = intervals.get(i);
if(curInterval.start > newInterval.end){
ret.add(newInterval);
@cangoal
cangoal / Pass by value in Java
Created June 3, 2015 14:58
Understand the "pass by value" in Java
// http://www.programcreek.com/2013/09/string-is-passed-by-reference-in-java/
public static void test (){
ArrayList<StringBuilder> ret = new ArrayList<StringBuilder>();
StringBuilder sb = new StringBuilder("abc");
ret.add(sb);
sb.append("d"); // sb = new StringBuilder("cba")
System.out.println(ret.toString());
ArrayList<String> lst = new ArrayList<String>();
@cangoal
cangoal / InsertInterval.java
Last active April 7, 2016 03:45
LeetCode - Insert Interval
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
ArrayList<Interval> ret = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0){
ret.add(newInterval);
return ret;
}
for(int i=0; i<intervals.size(); i++){
Interval curInterval = intervals.get(i);
if(curInterval.end < newInterval.start){
ret.add(curInterval);
@cangoal
cangoal / LengthofLastWord.java
Last active March 20, 2016 04:35
LeetCode - Length of Last Word
//
public int lengthOfLastWord(String s) {
if(s==null || s.length()==0) return 0;
int i=s.length()-1;
for(; i>=0; i--){
if(s.charAt(i) != ' ') break;
}
int j = i;
for(; j>=0; j--){
if(s.charAt(j) == ' ') break;
@cangoal
cangoal / Rotate List
Last active August 29, 2015 14:22
LeetCode - Rotate List
// consider case k =0 seperately
public ListNode rotateRight(ListNode head, int k) {
if(head == null) return head;
int size = 1;
ListNode cur = head;
while(cur.next != null){
cur = cur.next;
size++;
}
k = k % size;
@cangoal
cangoal / Unique Paths
Created June 3, 2015 18:47
LeetCode - Unique Paths
//
public int uniquePaths(int m, int n) {
if(m<1 || n<1) return 0;
if(m==1 || n==1) return 1;
int[][] ret = new int[m][n];
for(int i=0; i<m; i++){
ret[i][0] = 1;
}
@cangoal
cangoal / Unique Paths II
Created June 3, 2015 19:04
LeetCode - Unique Paths II
//
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
return 0;
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[] res = new int[n];
if(obstacleGrid[0][0] == 0) res[0] = 1;
else res[0] = 0;
for(int i=0;i<m;i++)
{
@cangoal
cangoal / Simplify Path
Created June 3, 2015 19:31
LeetCode - Simplify Path
public String simplifyPath(String path) {
if(path == null || path.length() == 0) return path;
String[] arr = path.split("/");
Stack<String> stack = new Stack<String>();
for(int i = 0; i < arr.length; i++){
if(arr[i].length() > 0){
if(arr[i].equals("..")){
if(!stack.isEmpty()) stack.pop();
} else if (arr[i].equals(".")){
continue;
@cangoal
cangoal / Combinations
Created June 3, 2015 20:17
LeetCode - Combinations
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
if(n <= 0 || k <= 0 || n < k) return ret;
helper(ret, n, k, 1, new ArrayList<Integer>());
return ret;
}
public void helper(ArrayList<ArrayList<Integer>> ret, int n, int k, int start , ArrayList<Integer> lst){
if(lst.size() == k){
ret.add(new ArrayList<Integer>(lst));
@cangoal
cangoal / Search in Rotated Sorted Array
Last active August 29, 2015 14:22
LeetCode - Search in Rotated Sorted Array
//
public int search(int[] nums, int target) {
if(nums == null || nums.length==0) return -1;
int left = 0, right = nums.length-1;
while(left <= right){
int mid = (left + right) / 2;
if(nums[mid] == target){
return mid;
}else if(nums[mid] >= nums[left]){
if(target >= nums[left] && nums[mid] > target){