Problem Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You ...

# Find First and Last Position of Element in Sorted Array

# Longest Valid Parentheses

Problem Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring. Examples Example 1: Input: s = “(()” Output: 2...

# Next Permutation

Problem A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are all the permutations of arr: ...

# Substring with Concatenation of All Words

Problem You are given a string s and an array of strings words. All the strings of words are of the same length. A concatenated substring in s is a substring that contains all the strings of any...

# Divide Two Integers

Problem Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losin...

# Find the Index of the First Occurrence in a String

Problem Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Examples Example 1: Input: haystack =...

# Remove Element

Problem Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the le...

# Remove Duplicates from Sorted Array

Problem Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be ke...

# First Missing Positive

Problem Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an algorithm that runs in O(n) time and uses constant extra space. Examples Exam...

# Reverse Nodes in k-Group

Problem Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked li...

# Swap Nodes in Pairs

Problem Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be ch...

# Generate Parentheses

Problem Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Examples Example 1: Input: n = 3 Output: [”((()))”,”(()())”,”(())()”,”()((...

# Merge Two Sorted Lists

Problem You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lis...

# Longest Common Prefix

Problem Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Examples Example 1: Input: strs = [“...

# Roman to Integer

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

# Remove Nth Node From End of List

Problem Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,4,5] Examp...

# 4Sum

Problem Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinc...

# Letter Combinations of a Phone Number

Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digits to lette...

# 3Sum Closest

Problem Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume t...

# 3Sum

Problem Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set ...

# Integer to Roman

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

# Regular Expression Matching

Problem Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of ...

# Palindrome Number

Problem Given an integer x, return true if x is a palindrome, and false otherwise. Examples Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from...

# String to Integer (atoi)

Problem mplement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function). The algorithm for myAtoi(string s) is as follows: Read in ...

# Reverse Integer

Problem Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume t...

# Zigzag Conversion

Problem 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 ...

# Longest Palindromic Substring

Problem Given a string s, return the longest palindromic substring in s. Examples Example 1: Input: s = “babad” Output: “bab” Explanation: “aba” is also a valid answer. Example 2: Inpu...

# Median of Two Sorted Arrays

Problem Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Examples Exam...

# Longest Substring Without Repeating Characters

Problem Given a string s, find the length of the longest substring without repeating characters. A substring is a contiguous non-empty sequence of characters within a string. Examples Example...

# Best Time to Buy and Sell Stock With Transaction Fee

Problem You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee. Find the maximum profit you can ach...

# Best Time to Buy and Sell Stock II

Problem You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most on...

# Best Time to Buy and Sell Stock

Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a diff...

# Path Sum II

Problem Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a li...

# Balanced Binary Tree

Problem Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than on...

# Symmetric Tree

Problem Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Examples Example 1: Input: root = [1,2,2,3,4,4,3] Output: true Exa...

# Same Tree

Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the n...

# Longest Repeating Character Replacement

Problem You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k time...

# Sliding Window Maximum

Problem You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the windo...

# Sliding Window Median

Problem The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values. For examples,...

# Partition Labes

Problem You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. Note that the partition is done so that after co...

# Container With Most Water

Problem You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that togethe...

# Top K Frequent Words

Problem Given an array of strings words and an integer k, return the k most frequent strings. Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequ...

# Top K Frequent Elements

Problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Examples Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Out...

# Remove Invalid Parentheses

Problem Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. Return a list of unique strings that are valid wi...

# Merge Intervals

Problem Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the ...

# Valid Parentheses

Problem Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the...

# Find All Anagrams in a String

Problem Given two strings s and p, return an array of all the start indices of p’s anagrams in s. You may return the answer in any order. An Anagram is a word or phrase formed by rearranging the...

# Number of Islands

Problem Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adja...

# Valid Anagram

Problem Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typic...

# Group Anagrams

Problem Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word ...

# Two Sum

Problem Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, ...

# Single Number

Problem Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only co...

# Search in Rotated Sorted Array II

Problem There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). Before being passed to your function, nums is rotated at an unknown pivot index k (0...

# Find Minimum in Rotated Sorted Array

Problem Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated ...

# Search in Rotated Sorted Array

Problem There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k &...

# Search a 2D Matrix

Problem You are given an m x n integer matrix matrix with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last in...

# Guess Number Higher or Lower

Problem We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the numb...

# Binary Search

Problem Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, r...

# Reverse Linked List

Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Examples Example 1: Input: [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1...

# Add Two Numbers

Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbe...

# Linked List Cycle

Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by co...

# Merge k Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples Example 1...

# Shortest Subarray with Sum at Least K

Problem Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1. A subarray i...

# Smallest Rotation with Highest Score

Problem You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. A...

# Subarray Sum Equals K

Problem Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. A subarray is a contiguous non-empty sequence of elements within an array. ...

# Continuous Subarray Sum

Problem Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. A good subarray is a subarray where: its length is at least two, and the s...

# Max Sum of Rectangle No Larger Than K

Problem Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k. It is guaranteed that there will be a rectangle with...

# Range Sum Query 2D - Immutable

Problem Given a 2D matrix matrix, handle multiple queries of the following type: Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1)...

# Range Sum Query - Immutable

Problem Given an integer array nums, handle multiple queries of the following type: Calculate the sum of the elements of nums between indices left and right inclusive where left <= right. Im...

# Product of Array Except Self

Problem Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is...

# Minimum Size Subarray Sum

Problem Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray,...