Title: Kth Smallest Element in a BST Source: leetcode.com Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need ...
Title: Implement Stack using Queues Source: leetcode.com Implement the following operations of a stack using queues. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. empty() — Return whether the stack is empty. Notes: You must use only standard operations of ...
Title: Implement Queue using Stacks Source: leetcode.com Implement the following operations of a queue using stacks. push(x) — Push element x to the back of queue. pop() — Removes the element from in front of queue. peek() — Get the front element. empty() — Return whether the queue is empty. Notes: You must use only ...
Title: House Robber Source: leetcode.com You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses ...
Title: Gray Code Source: leetcode.com The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = ...
Title: First Bad Version Source: leetcode.com You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have ...
Title: Course Schedule Source: leetcode.com There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and ...
Title: Count and Say Source: leetcode.com The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an ...
Title: Copy List with Random Pointer Source: leetcode.com A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. Java solution Java /* https://leetcode.com/problems/copy-list-with-random-pointer/ */ import java.util.HashMap; public class CopyListWithRandomPointers { public RandomListNode copyRandomList(RandomListNode ...
Title: Contains Duplicate III Source: leetcode.com Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. Java solution Java /*https://leetcode.com/problems/contains-duplicate-iii/*/ public class ContainsDuplicateIII ...