two-sumsV2.cpp (799B)
1 #include <iostream> 2 #include <vector> 3 4 //Leet Ratings 5 // Speed Memory 6 //Total 79ms 10.2MB 7 //Beats 50.84% 73.65% 8 // 9 //This is so fast because it does a reverse search and one of the test cases 10 //has a ton of numbers where the correct indexes are at the end. 11 using namespace std; 12 13 vector<int> twoSum(vector<int>& nums, int target) { 14 15 const int length = nums.size() - 1; 16 for(int i = length; i >= 0 ; --i){ 17 for (int x = length; x >= 0 ; --x){ 18 if( nums[x] + nums[i] == target & x != i){ 19 return{x,i}; 20 } 21 } 22 } 23 return {}; 24 } 25 26 27 int main(){ 28 29 30 vector<int> sums = {3, 2, 4}; 31 twoSum(sums, 6); 32 cout << twoSum(sums, 6)[0] << twoSum(sums, 6) [1] << endl; 33 34 } 35