Difficulty: Easy
Category: Array
Description:
Given a sorted array of integers nums
and an integer target
, return the indices (1-based) of the two numbers such that they add up to target
.
Constraints:
2 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
nums
is sorted in non-decreasing order- There is exactly one solution
Function Signature:
function twoSumSorted(nums, target) {
// your code here
}
Example Tests:
twoSumSorted([2, 7, 11, 15], 9); // [1, 2]
twoSumSorted([1, 2, 3, 4, 6], 6); // [2, 4]
twoSumSorted([-3, 0, 2, 4, 10], 7); // [3, 5]
Difficulty: Easy
Category: Array
Description:
Given a binary array nums
, return the maximum number of consecutive 1's in the array.
Constraints:
1 <= nums.length <= 10^5
nums[i]
is either 0 or 1
Function Signature:
function findMaxConsecutiveOnes(nums) {
// your code here
}
Example Tests:
findMaxConsecutiveOnes([1,1,0,1,1,1]); // 3
findMaxConsecutiveOnes([1,0,1,1,0,1]); // 2
findMaxConsecutiveOnes([0,0,0]); // 0
Difficulty: Easy
Category: Hash Table
Description:
Given a string s
, find the first non-repeating character and return its index. If it does not exist, return -1.
Constraints:
1 <= s.length <= 10^5
s
consists of only lowercase English letters
Function Signature:
function firstUniqChar(s) {
// your code here
}
Example Tests:
firstUniqChar("leetcode"); // 0
firstUniqChar("loveleetcode"); // 2
firstUniqChar("aabb"); // -1
Difficulty: Medium
Category: Hash Table
Description:
Given an array of strings strs
, group the anagrams together. You can return the answer in any order.
Constraints:
1 <= strs.length <= 10^4
0 <= strs[i].length <= 100
strs[i]
consists of lowercase English letters
Function Signature:
function groupAnagrams(strs) {
// your code here
}
Example Tests:
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);
// [["eat","tea","ate"],["tan","nat"],["bat"]]
groupAnagrams([""]);
// [[""]]
groupAnagrams(["a"]);
// [["a"]]
Difficulty: Medium
Category: Divide and Conquer
Description:
Suppose an array of length n
sorted in ascending order is rotated between 1
and n
times. Given the array nums
and an integer target
, return the index of target
if it is in nums
, or -1 if it is not.
Constraints:
1 <= nums.length <= 5000
-10^4 <= nums[i] <= 10^4
- All values of
nums
are unique nums
is rotated at some pivot
Function Signature:
function searchRotated(nums, target) {
// your code here
}
Example Tests:
searchRotated([4,5,6,7,0,1,2], 0); // 4
searchRotated([4,5,6,7,0,1,2], 3); // -1
searchRotated([1], 0); // -1
Difficulty: Hard
Category: Divide and Conquer
Description:
You are given an array of k
sorted arrays, merge them into one sorted array.
Constraints:
1 <= k <= 100
0 <= arrays[i].length <= 1000
-10^4 <= arrays[i][j] <= 10^4
Function Signature:
function mergeKSortedArrays(arrays) {
// your code here
}
Example Tests:
mergeKSortedArrays([[1,4,5],[1,3,4],[2,6]]);
// [1,1,2,3,4,4,5,6]
mergeKSortedArrays([[]]);
// []
mergeKSortedArrays([[1], [0]]);
// [0,1]