Thursday, June 5, 2014

LeetCode Remove Duplicates from Sorted List

public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode curr = head;
        ListNode next = head.next;
        while (next != null) {
            if (curr.val == next.val) {
                curr.next = next.next;
                next = curr.next;
            } else {
                curr = next;
                next = curr.next;
            }
        }
        return head;
    }

LeetCode Reverse Nodes in K-Group

Key part is reverse method.


  public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k == 1) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode prev = dummy;
        int i = 0;
        while (head != null) {
            i++;
            if (i%k == 0) {
                prev = reverse(prev, head.next);
                head = prev.next;
            } else {
                head = head.next;
            }
        }
        return dummy.next;
    }
    private ListNode reverse(ListNode prev, ListNode next) {
        ListNode last = prev.next;
        ListNode curr = last.next;
        while (curr != next) {
            last.next = curr.next;
            curr.next = prev.next;
            prev.next = curr;
            curr = last.next;
        }
        return last;
    }

LeetCode Reverse Linked List II

The key part is "reverse" method.
Also it can be used in the Reverse K group.

public ListNode reverseBetween(ListNode head, int m, int n) {
       ListNode dummy = new ListNode(-1);
       dummy.next = head;
       ListNode temp = dummy;
       int i = 1;
       //Find the prev
       while( temp != null && i < m) {
           temp = temp.next;
           i++;
       }
       ListNode prev = temp;
       //Find the next
       while (temp != null && i <= n) {
           temp = temp.next;
           i++;
       }
       ListNode next = temp.next;
       reverse(prev, next);
       return dummy.next;
    }
    private void reverse(ListNode prev, ListNode next) {
        ListNode last = prev.next;
        ListNode curr = last.next;
        while (curr!=next) {
            last.next = curr.next;
            curr.next = prev.next;
            prev.next = curr;
            curr = last.next;
        }
    }

Wednesday, June 4, 2014

LeetCode Letter Combinations of a Phone Number

DFS

 public List<String> letterCombinations(String digits) {
       List<String> result = new ArrayList<String>();
       if (digits == null || digits.length() == 0) {
           result.add("");
           return result;
       }
       String[] map = {"abc","def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
       //DFS
       for (String rest : letterCombinations(digits.substring(1))){
           for (char c : map[digits.charAt(0) - '0' - 2].toCharArray()) {
               result.add(c + rest);
           }
       }
       return result;
    }

Tuesday, June 3, 2014

LeetCode Remove Nth Node From the End of List

Boundary Conditions:
1. Only one node in the list
2. Remove the first node

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode front = head;
        ListNode behind = head;
        //Only one node in the list
        if (head.next == null) return null;
        //Move the front Node n steps
        while (n > 0) {
            front = front.next;
            n--;
        }
        //Remove the first node
        if (front==null) {
            head = head.next;
            return head;
        }
        //Move front and behind at the same time
        while (front.next != null) {
            front = front.next;
            behind = behind.next;
        }
       
        behind.next = behind.next.next;
        return head;
    }

LeetCode Longest Common Prefix

Here we need check the length of the string in the array.

public String longestCommonPrefix(String[] strs) {
        if (strs== null || strs.length == 0) return "";
        StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs[0].length(); i++) {
char temp = strs[0].charAt(i);
for (int j = 0; j < strs.length; j++) {

if (i >= strs[j].length() || temp != strs[j].charAt(i)) {
return sb.toString();
}
}
sb.append(temp);
}
return sb.toString();
    }

Leetcode Roman to Integer

public int romanToInt(String s) {
        //put all the symbols and numbers in the hashmap
        int[] numbers = {1000, 500, 100, 50, 10, 5, 1};
        char[] symbols = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < numbers.length; i++) {
            map.put(symbols[i], numbers[i]);
        }
       
        //Calculate the integer
        char[] charArr = s.toCharArray();
        int result = map.get(charArr[s.length() - 1]);
        for (int i = 0; i < s.length()-1; i++) {
            result += map.get(charArr[i])
                        *(map.get(charArr[i]) >= map.get(charArr[i+1]) ? 1 : -1);
        }
        return result;
    }