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 boolean search(int[] nums, int target) { | |
| if(nums == null || nums.length == 0) return false; | |
| int left = 0, right = nums.length - 1; | |
| while(left <= right){ | |
| while(nums[left] == nums[right] && left < right){ | |
| right--; | |
| } | |
| int mid = (left + right) / 2; | |
| if(nums[mid] == target) return true; |
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 ListNode deleteDuplicates(ListNode head) { | |
| if(head == null) return head; | |
| ListNode pre = head, cur = head.next; | |
| while(cur != null){ | |
| if(cur.val != pre.val){ | |
| cur = cur.next; | |
| pre = pre.next; | |
| } else { | |
| pre.next = cur.next; |
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 ListNode deleteDuplicates(ListNode head) { | |
| if(head == null) return head; | |
| ListNode dummyNode = new ListNode(0); | |
| dummyNode.next = head; | |
| ListNode pre = dummyNode; | |
| while(head != null && head.next != null){ | |
| if(head.val == head.next.val){ | |
| while(head.next != null && head.val == head.next.val){ | |
| head = head.next; |
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 ListNode partition(ListNode head, int x) { | |
| if(head == null) return head; | |
| ListNode dummyNode = new ListNode(0); | |
| dummyNode.next = head; | |
| ListNode first = dummyNode, second = null; | |
| while(head != null){ | |
| if(head.val >= x){ | |
| if(second == null){ | |
| second = head; |
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<Integer> grayCode(int n) { | |
| ArrayList<Integer> ret = new ArrayList<Integer>(); | |
| if(n < 0) return ret; | |
| ret.add(0); | |
| if(n == 0) return ret; | |
| for(int i=0; i<n; i++){ | |
| ArrayList<Integer> temp = new ArrayList<>(); | |
| for(int j=ret.size()-1; j>=0; j--){ | |
| temp.add((1<<i) + ret.get(j)); // the priority of +/- operation is higher then >>/<< shift operation |
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 ListNode newhead = null; | |
| public ListNode reverseList(ListNode head) { | |
| if(head == null) return head; | |
| helper(head, head.next); | |
| return newhead; | |
| } | |
| public void helper(ListNode pre, ListNode cur){ | |
| if(cur == null){ |
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 ListNode reverseBetween(ListNode head, int m, int n) { | |
| if(head == null || m == n) return head; | |
| ListNode dummyNode = new ListNode(0); | |
| dummyNode.next = head; | |
| ListNode walker = dummyNode, runner = dummyNode; | |
| int i=1; | |
| while(i < m){ | |
| walker = walker.next; | |
| runner = runner.next; |
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<String>> partition(String s) { | |
| ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>(); | |
| if(s == null || s.length()==0) return ret; | |
| helper(ret, s, new ArrayList<String>()); | |
| return ret; | |
| } | |
| public void helper(ArrayList<ArrayList<String>> ret, String s, ArrayList<String> lst){ | |
| if(s.length() == 0){ | |
| ret.add(new ArrayList<String>(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 canCompleteCircuit(int[] gas, int[] cost) { | |
| if(gas == null || gas.length == 0 || cost == null || cost.length == 0) return -1; | |
| int total = 0, local = 0, index = 0; | |
| for(int i=0; i<gas.length; i++){ | |
| total += gas[i] - cost[i]; | |
| local += gas[i] - cost[i]; | |
| if(local < 0){ | |
| index = i + 1; // don't write index = i; | |
| local = 0; |
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 candy(int[] ratings) { | |
| if(ratings == null || ratings.length == 0) return 0; | |
| int[] candy = new int[ratings.length]; | |
| candy[0] = 1; | |
| for(int i=1; i<candy.length; i++){ | |
| if(ratings[i] > ratings[i-1]){ | |
| candy[i] = candy[i-1] + 1; | |
| } else { | |
| candy[i] = 1; |