-
Notifications
You must be signed in to change notification settings - Fork 1
Array
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.
#include <unordered_map>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> mp;
for(int i = 0; i < nums.size(); i++){
if(mp.find(target - nums[i]) == mp.end())
mp[nums[i]] = i;
else
return {mp[target - nums[i]], i};
}
return {-1, -1};
}
};
Time/Space: O(N)/O(N)
*153: Find Minimum in Rotated Sorted Array 31: Next Permutation 34: Find First and Last Position of Element in Sorted Array 41: First Missing Positive 42: Trapping Rain Water 84: Largest Rectangle in Histogram 88: Merge Sorted Array 283: Move Zeroes 349: Intersection of Two Arrays 380: Insert Delete GetRandom O(1) 399: Evaluate Division 452: Minimum Number of Arrows to Burst Balloons 529: Minesweeper 560: Subarray Sum Equals K 973: K Closest Points to Origin 977: Squares of a Sorted Array 994: Rotting Oranges 1010: Pairs of Songs With Total Durations Divisible by 60 1095: Find in Mountain Array 1169: Invalid Transactions 1235: Maximum Profit in Job Scheduling 1293: Shortest Path in a Grid with Obstacles Elimination