Merge Two Binary Trees

Problem You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. ...

Palindrome Linked List Problem

Problem Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Examples Example 1: Input: head = [1,2,2,1] Output: true head = 1 -> 2 -> 2 -> 1 Example 2: Input: head = [1,2] Output: true head = 1 -> 2 ...

Maximum Product of Three Numbers Problem

Problem Given an integer array nums, find three numbers whose product is maximum and return the maximum product. Examples Example 1: Input: nums = [1,2,3] Output: 6 Example 2: Input: nums = [1,2,3,4] Output: 24 ...

Fizz Buzz Problem

Fizz Buzz Problem Problem Given an integer n, return a string array answer (1-indexed) where: answer[i] == "FizzBuzz" if i is divisible by 3 and 5. answer[i] == "Fizz" if i is divisible by 3. answer[i] == "Buzz" if i is divisible by 5. answer[i] == i (as a string) if none of the above conditions are true. Examples Example 1: Input: n = 3 Output: ["1","2","Fizz"] ...

Make Array Zero by Subtracting Equal Amounts

Problem You are given a non-negative integer array nums. In one operation, you must: Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums. Subtract x from every positive element in nums. Return the minimum number of operations to make every element in nums equal to 0. Examples Example 1: Input: nums = [1,5,0,3,5] Output: 3 Explanation: In the first operation, choose x = 1. Now, nums = [0,4,0,2,4]. In the second operation, choose x = 2. Now, nums = [0,2,0,0,2]. In the third operation, choose x = 2. Now, nums = [0,0,0,0,0]. ...

Design HashMap Problem

Design HashMap Problem Problem Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value. int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. void remove(key) removes the key and its corresponding value if the map contains the mapping for the key. Examples Example 1: ...

Design Hashset Problem

Design Hashset Problem Problem Design a HashSet without using any built-in hash table libraries. Implement MyHashSet class: void add(key) Inserts the value key into the HashSet. bool contains(key) Returns whether the value key exists in the HashSet or not. void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing. Examples Example 1: Input ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"] [ [], [1], [2], [1], [3], [2], [2], [2], [2] ] Output [null, null, null, true, false, null, true, null, false] Explanation MyHashSet myHashSet = new MyHashSet(); myHashSet.add(1); // set = [1] myHashSet.add(2); // set = [1, 2] myHashSet.contains(1); // return True myHashSet.contains(3); // return False, (not found) myHashSet.add(2); // set = [1, 2] myHashSet.contains(2); // return True myHashSet.remove(2); // set = [1] myHashSet.contains(2); // return False, (already removed) ...

Ugly Number Problem

Problem Write a program to check whether a given number is an ugly number. Ugly Number Definition Examples Example 1: Input: n = 6 Output: true Explanation: 6 = 2 × 3 Example 2: Input: n = 1 Output: true Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5. ...

Add Digits in Number Problem

Problem Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. Examples Example 1: Input: num = 38 Output: 2 Explanation: The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. ...

Implement Power Function 2

Problem Implement pow(x, n) % d. In other words, given x, n and d, find (x^n % d) Note that remainders on division cannot be negative. In other words, make sure the answer you return is non negative. Examples Example 1: Input: x = 2, n = 3, d = 3 Output: 2 Explanation: 2^3 % 3 = 8 % 3 = 2. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy