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

LeetCode – Median of Two Sorted Arrays (Java)

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java Solution This problem can be converted to the problem of finding kth element, k is (A’s length + B’ Length)/2. If any of … Read more

LeetCode – Clone Graph (Java)

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. Java Solution 1 – DFS HashMap<Node, Node> map = new HashMap<>(); public Node cloneGraph(Node node) { map.put(node, new Node(node.val, new ArrayList<>()));   for(Node neighbor: node.neighbors){ if(map.containsKey(neighbor)){ map.get(node).neighbors.add(map.get(neighbor)); }else{ map.get(node).neighbors.add(cloneGraph(neighbor)); } }   return map.get(node); }HashMap<Node, Node> map … Read more

LeetCode – Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that only one letter can be changed at a time and each intermediate word must exist in the dictionary. For example, given: start = “hit” end = “cog” dict = [“hot”,”dot”,”dog”,”lot”,”log”] One shortest transformation … Read more

LeetCode – Reverse Integer

LeetCode – Reverse Integer: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 1. Naive Method We can convert the integer to a string/char array, reverse the order, and convert the string/char array back to an integer. However, this will require extra space for the string. It … Read more

Leetcode – Linked List Cycle

Given a linked list, determine if it has a cycle in it. Analysis If we have 2 pointers – fast and slow. It is guaranteed that the fast one will meet the slow one if there exists a circle. The problem can be demonstrated in the following diagram: Java Solution public class Solution { public … Read more

Leetcode – Word Break (Java)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = “leetcode”, dict = [“leet”, “code”]. Return true because “leetcode” can be segmented as “leet code”. 1. Naive Approach This problem can be solve by … Read more