Follow us Likes Follows us

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: adminInterview  <LeetCode> Day 1 - #1 Two Sum
#1
https://leetcode.com/problems/two-sum/description/

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
---------------------------------------------------------------------------------------------------------------------------------------------
 Thoughts:

[2,7,11,15], target = 9
2 + 7 = 9

[2,7,11,15], target = 22

2 +7 = 9; 2 + 11 = 13; 2 + 15  = 17; 
7 + 11 = 18
7 + 15 = 22;

------------------------------------------------------------------------------

Code:
// Example program
#include <iostream>
#include <string>
using namespace std;

void twoSum ( int arr[], int arrSize, int target)
{
 
   int* a = new int[2];
   for(int i = 0 ;i < arrSize; i++)
   {
       for(int j = i+ 1; j < arrSize;j++)
       {
           if (arr[i] + arr[j] == target)
           {
               
               cout << arr[i]<< " "<<arr[j];
           }
       }
   }
   cout << " = "<< target<<endl;
}


int main()
{
   int arr[4] = { 2, 7, 11, 15};
   twoSum(arr,4,9);
}
Everyday is the 1st day of your life!
So make it beautiful!
Heart
Reply
#2
// my_map1.insert(make_pair("earth", 1));
// my_map1.insert(make_pair("moon", 2));
// map<string,int>::iterator it;
// for(it = my_map1.begin(); it != my_map1.end(); it++)
// {
// cout << it->first<<" ---- " <<it->second<<endl;
// }




//
// map<int, string> my_map;
// my_map[1] = "Tom";
// my_map[2] = "Mack";
// my_map[3] = "Jose";
// my_map[4] = "Xiao";
// my_map[5] = "Xiaolong";
//
// my_map.insert(pair<int, string> (6, "Assaf"));
// my_map.insert(pair<int, string> (7, "Ehuds"));
//
// //map<int, string>::iterator it = my_map.find(5);
//// cout << "Key found = " << my_map.find(2) -> second;
//// cout << "Empty? " << my_map.empty()<<endl;
////
////
//// cout <<"Map size = " << my_map.size()<< "\n";
// map<int, string>::iterator iterator;
// for( iterator = my_map.begin(); iterator != my_map.end();iterator++)
// {
// cout << iterator->first <<" "<<iterator->second << "\n";
// }
Everyday is the 1st day of your life!
So make it beautiful!
Heart
Reply




Users browsing this thread: 1 Guest(s)