🎯 Arrays & Hashing
1. Two Sum
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, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]Constraints:
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9Only one valid answer exists.
Follow-up:
Can you come up with an algorithm that is less than O(n^2) time complexity?
2. Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Example 1:
Example 2:
Constraints:
1 <= s.length, t.length <= 5 * 10^4sandtconsist of lowercase English letters.
Follow up:
What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
3. Ransom Note
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= ransomNote.length, magazine.length <= 10^5ransomNoteandmagazineconsist of lowercase English letters.
Follow up:
Could you solve it in O(n + m) time complexity?
4. Majority Element
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example 1:
Example 2:
Constraints:
n == nums.length1 <= n <= 5 * 10^4-2^31 <= nums[i] <= 2^31 - 1
Follow up:
Could you solve the problem in linear time and in O(1) space?
5. Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Follow up:
Could you solve the problem in linear time and in O(1) space?
6. Move Zeroes
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Example 1:
Example 2:
Constraints:
1 <= nums.length <= 10^4-2^31 <= nums[i] <= 2^31 - 1
Follow up:
Could you solve this problem in O(n) time complexity?
7. Missing Number
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Example 1:
Example 2:
Example 3:
Constraints:
n == nums.length1 <= n <= 10^40 <= nums[i] <= nAll the numbers of
numsare unique.
Follow up:
Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
8. Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Example 1:
Example 2:
Constraints:
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4numsis sorted in non-decreasing order.
Follow up:
Squaring each element and sorting the new array is very simple, could you find an O(n) solution using a different approach?
Last updated