Category Archives: Array and linked list

Median of Two Sorted Arrays

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)). C++: 01 double kSmall(int A[],int m,int B[],int n,int k) 02 { … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. C++: 01 class Solution { 02 public: 03     string longestPalindrome(string s) { 04      … Continue reading

Posted in Array and linked list, String | Leave a comment

ZigZag Conversion

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G … Continue reading

Posted in Array and linked list, String | Leave a comment

Container With Most Water

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container … Continue reading

Posted in Array and linked list, Greedy algorithm | Leave a comment

4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ? b ? c ? d) The solution set must not contain … Continue reading

Posted in Array and linked list | Leave a comment

3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S … Continue reading

Posted in Array and linked list | Leave a comment

3Sum

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) The solution set must not contain duplicate … Continue reading

Posted in Array and linked list | Leave a comment

Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening … Continue reading

Posted in Array and linked list, Hashtable and Map, String | Leave a comment

Longest Common Prefix

Write a function to find the longest common prefix string among an array of strings. C++: 01 class Solution { 02 public: 03     string longestCommonPrefix(vector<string> &strs) { 04         // Start typing your C/C++ solution below 05      … Continue reading

Posted in Array and linked list, String | Leave a comment

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length … Continue reading

Posted in Array and linked list, String | Leave a comment