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

Title: Contains Duplicate II Source: leetcode.com Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. Java solution Java /* https://leetcode.com/problems/contains-duplicate-ii/ */ import java.util.HashMap; public class ...

Title: Contains Duplicate Source: leetcode.com Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Java solution Java /* https://leetcode.com/problems/contains-duplicate/ */ import java.util.HashSet; public class ContainsDuplicate { public ...

Title: Container With Most Water Source: leetcode.com 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 ...

Title: Clone Graph Source: leetcode.com Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. Nodes are labeled uniquely. Java solution Java /* https://leetcode.com/problems/clone-graph/ */ import java.util.HashMap; import java.util.List; import java.util.ArrayList; public class CloneGraph { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node==null) return null; HashMap<Integer, UndirectedGraphNode> visited = ...

Title: Climbing Stairs Source: leetcode.com You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Java solution Java /* https://leetcode.com/problems/climbing-stairs/ */ import java.util.HashSet; class ClimbStairs { HashSet set = ...