Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / SearchinRotatedSortedArrayII.java
Last active March 15, 2016 20:52
LeetCode - Search in Rotated Sorted Array II
//
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;
@cangoal
cangoal / RemoveDuplicatesfromSortedList.java
Last active March 16, 2016 02:13
LeetCode - Remove Duplicates from Sorted List
//
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;
@cangoal
cangoal / RemoveDuplicatesfromSortedListII.java
Last active March 16, 2016 02:12
LeetCode - Remove Duplicates from Sorted List II
//
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;
@cangoal
cangoal / Partition List
Created June 4, 2015 15:26
LeetCode - Partition List
//
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;
@cangoal
cangoal / Gray Code
Created June 4, 2015 17:16
LeetCode - Gray Code
//
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
@cangoal
cangoal / ReverseLinkedList.java
Last active March 16, 2016 13:43
LeetCode - Reverse Linked List
//
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){
@cangoal
cangoal / Reverse Linked List II
Created June 4, 2015 18:50
LeetCode - Reverse Linked List II
//
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;
@cangoal
cangoal / Palindrome Partitioning
Created June 4, 2015 19:14
LeetCode - Palindrome Partitioning
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));
@cangoal
cangoal / Gas Station
Created June 4, 2015 19:42
LeetCode - Gas Station
//
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;
@cangoal
cangoal / Candy
Created June 4, 2015 20:01
LeetCode - Candy
//
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;