Saturday, September 19, 2020

1. Two Sum LeetCode

C++ STL map solution O(nlogn)


class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map< int ,int > mp;
vector< int > ans(2, -1);
for(int i=0; i< (int)nums.size(); i++ ){
if( mp.find( target - nums[i] ) != mp.end() ){
return { i, mp[ target - nums[i] ] };
}
mp[ nums[i] ] = i;
}
return ans;
}
};


No comments:

Post a Comment

3. Longest Substring Without Repeating Characters LeetCode

 Time Complexity: O(N)  class Solution { public: int lengthOfLongestSubstring (string s) { vector< int > c...