1. Java inheritance
2. Two Sum
3. 各种sort以及他们的runtime
4. Implement merge sort
5. Stack和Queue的运用
6. Find duplicate:
Leetcode #83: Remove Duplicates from Sorted List -> 如果有多余的,只保留其中一个即可
public ListNode deleteDuplicates(ListNode head) { ListNode current = head; while(current != null){ while(current.next != null && current.val == current.next.val){ current.next = current.next.next; } current = current.next; } return head; }
Leetcode #82: Remove Duplicates from Sorted List II -> 如果有多余的,所有有关于这个element的node都要删掉
public ListNode deleteDuplicates(ListNode head) { ListNode fake = new ListNode(-1); fake.next = head; ListNode pre = fake; ListNode cur = head; while(cur != null){ while(cur.next != null && cur.val == cur.next.val){ cur = cur.next; } if(pre.next == cur){ pre = pre.next; }else{ pre.next = cur.next; cur = cur.next; } } return fake.next; }
7. Leetcode #33: Search in Rotated Sorted Array
public int search(int[] nums, int target) { int l = 0, r = nums.length - 1, m = (l + r)/2, result = -1; while(l <= r){ m = (l + r) / 2; if(nums[m] == target){ result = m; break; }else if(l == r){ break; } if((nums[m] <= nums[r] && target > nums[m] && target <= nums[r]) || (nums[l] <= nums[m] && (target > nums[m] || target < nums[l]))){ l = m + 1; }else{ r = m; } } return result; }
8. Leetcode #15: 3Sum
public List> threeSum(int[] nums) { List
> result = new ArrayList<>(); if(nums.length < 3){ return result; } Arrays.sort(nums); if(nums[0] > 0 || nums[nums.length-1] < 0){ return result; } for(int i = 0; i < nums.length - 2; i++){ if(i == 0 || nums[i - 1] != nums[i]){ int j = i + 1; int k = nums.length - 1; while(j < k){ if(nums[i] == - (nums[j] + nums[k])){ List
current = new ArrayList<>(); current.add(nums[i]); current.add(nums[j]); current.add(nums[k]); result.add(current); // check repeated values while(j < k && nums[j] == nums[j + 1]){ j++; } while(j < k && nums[k] == nums[k - 1]){ k--; } j++; k--; }else if(nums[i] < - (nums[j] + nums[k])){ // nums[j] + nums[k] can be larger j++; }else{ k--; } } } } return result; }