LeetCode – Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. Thoughts The key of this problem is using the right loop condition. And change what is necessary in each loop. You can use different iteration conditions like the following 2 … Read more

LeetCode – Insert Interval

Problem: Given a set of non-overlapping & sorted intervals, insert a new interval into the intervals (merge if necessary). Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2: Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16]. This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]. Java Solution 1 … Read more

LeetCode – Pow(x, n)

Problem: Implement pow(x, n). This is a great example to illustrate how to solve a problem during a technical interview. The first and second solution exceeds time limit; the third and fourth are accepted. Java Solution public double myPow(double x, int n){ if(n==0) return 1;   if(n<0){ return 1/helper(x, -n); }   double v = … Read more

LeetCode – Implement strStr() (Java)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Java Solution 1 – Naive public int strStr(String haystack, String needle) { if(haystack==null || needle==null) return 0;   if(needle.length() == 0) return 0;   for(int i=0; i<haystack.length(); i++){ if(i + needle.length() > … Read more

LeetCode – Valid Parentheses (Java)

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not. Analysis A typical problem which can be solved by using a stack data structure. … Read more

LeetCode – Merge Sorted Array (Java)

Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Analysis The key to solve this problem is moving element … Read more

LeetCode – Merge Two Sorted Lists (Java)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Java Solution The key to solve the problem is defining a fake head. Then compare the first elements from each list. Add the smaller one to the … Read more

LeetCode – String to Integer (atoi) (Java)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Analysis The following cases should be considered for this problem: 1. null or empty string 2. white spaces 3. +/- … Read more

LeetCode – 3Sum

Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) … Read more

LeetCode – Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Analysis The key to solve this problem is defining a Comparator first to sort the arraylist of Intevals. Java Solution public List<Interval> merge(List<Interval> intervals) { if(intervals == null || intervals.size()<=1){ return intervals; }   Collections.sort(intervals, Comparator.comparing((Interval itl)->itl.start));   List<Interval> result … Read more

LeetCode – Regular Expression Matching (Java)

Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch(“aa”,”a”) return false isMatch(“aa”,”aa”) return true isMatch(“aaa”,”aa”) … Read more