This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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++) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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){ |