two-sums.cpp (1155B)
1 #include <iostream> 2 #include <vector> 3 4 //I hate that I have to add this line 5 using namespace std; 6 7 //Leet Ratings 8 // Speed Memory 9 //Total 675ms 10.2MB 10 //Beats 6.87% 73.65% 11 // 12 //This is so slow because it, among other things, does not do a reverse search and one of the test cases 13 //has a ton of numbers where the correct indexes are at the end. 14 15 16 17 vector<int> twoSum(vector<int>& nums, int target) { 18 19 int first = 0; 20 int second = 0; 21 bool done = false; 22 int length = nums.size(); 23 for(int i = 0 ; i < length ; ++i){ 24 if(done == true){ 25 break; 26 } 27 for (int x = 0; x < length ; ++x){ 28 29 30 if( nums[x] + nums[i] == target & x != i){ 31 done = true; 32 first = x; 33 second = i; 34 35 break; 36 } 37 38 39 } 40 41 42 43 } 44 45 vector<int> vals = {first , second}; 46 return (vals); 47 } 48 49 50 int main(){ 51 52 53 vector<int> sums = {3, 2, 4}; 54 twoSum(sums, 6); 55 cout << twoSum(sums, 6)[0] << twoSum(sums, 6) [1] << endl; 56 57 }