Category Archives: algorithm

Mini Parser

leetcode 385. Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list — whose elements may also be integers or other lists. Note: You may assume that the string is well-formed: String is non-empty. String does not contain white spaces.… Read More »

Lexicographical Numbers

leetcode 386. Given an integer n, return 1 – n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. Subscribe to see which companies asked this question Solution. A great solution is from here. The result… Read More »

Wiggle Subsequence

leetcode 376. A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence. For example, [1,7,4,9,2,5] is a wiggle sequence because… Read More »

Multiply String

leetcode 43. Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. Converting the input string to integer is NOT allowed. You should NOT use internal library such as BigInteger. Solution. An awesome solution is from this post. length of new… Read More »

Insert Delete GetRandom O(1) – Duplicates allowed

leetcode 381. Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom: Returns a random element from current collection of elements. The probability of each element being returned… Read More »

Insert Delete GetRandom O(1)

leetcode 380. Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from current set of elements. Each element must have the same probability of… Read More »

3 Sum

leetcode 15. 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: The solution set must not contain duplicate triplets. For example, given array S = [-1,… Read More »

Longest Substring Without Repeating Characters

leetcode 3 Given a string, find the length of the longest substring without repeating characters. Examples: Given “abcabcbb”, the answer is “abc”, which the length is 3. Given “bbbbb”, the answer is “b”, with the length of 1. Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be… Read More »

Min Stack

leetcode 155. There are many ways to solve the min stack problem. However, there is one solution which only requiring O(1) extra space. The idea is that when pushinng, store the offset of the current min value. When pop or getTop, use the offset and min to get the top or next min. If the… Read More »

Search in Rotated Sorted Array I & II

leetcode 33, 81 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. What if duplicates… Read More »