TWO SUM
https://leetcode.com/problems/two-sum/
1. Brute Force -
TC = O(n**2)
AS = O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
vector<int>res;
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if((nums[j]) == (target - nums[i])){
res.push_back(i);
res.push_back(j);
break;
}
}
}
return res;
}
};
No comments:
Post a Comment