Preface
As near graduation to find a job, although read the data structure and algorithm, but always feel the heart hollow. And the project is exposed to programming is based on other aspects of the improved algorithm, in short I have limited programming ability, often despised, so determined to crawl slowly, a little brush leetcode, which is also write these blog reasons, recorded in the encounter problems when the analysis of ideas, and solutions.
----------I walk slowly, but I never retreat.
=============================================================================================================== =======================
Topic 1:
Given an array of integers, find the numbers such that they add up to a specific target number.
The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.
You may assume this each input would has exactly one solution.
Input: numbers={2, 7, one, target=9
Output: index1=1, index2=2
=============================
Folly:
The main purpose of the topic is to give a sequence of integers, giving a target integer, which requires finding out the number of two numbers in the sequence to be the subscript of the target, and the small subscript in front.
To see this topic, the first idea is a two-layer loop, the outer layer is used to traverse the sequence, from the loop in the elapsed time to determine whether the outer layer corresponds to the elements corresponding to the inner element is equal to the target number, according to this idea, but after the submission, the prompt to the limit exceed. Code as follows:
/************************************************************************* > File Name:twoSum.cc > Author:je Rry Shi > Mail: [email protected] > Created time:2014 December 27 Saturday 10:52 32 sec. **************************** /#include <iostream> #include <vector>using namespace std; Class solution{public:vector<int> Twosum (vector<int> &numbers,int target) {vector<int> result; int length = Numbers.size () -1;int index1 = -1;int Index2 =-1; int i,j;for (i=0; I <= length; ++i) {for (j = i + 1; J <= length; ++j) if (target = = (Numbers[i] + num Bers[j]) {result.push_back (i+1);//index1 result.push_back (j+1);//index2}} if (!result.empty ()) return result;elsecout << "No Property index in given vector!" << Endl;} };int main (void) {vector<int> test; Vector<int> Result;test.push_back (2); TEST.push_back (7); Test.push_back (one); Test.push_back (+); int target = 9; Solution Mtest;result = mtest.twosum (test,target); int i;for (i = 0; i < result.size (); i+=2) {cout << "index1 =" << result.at (i) << "\ T" << "index 2 = "<< result.at (i+1) << Endl;} return 0;}
It is obvious that the purpose of this topic is mainly to examine the efficiency of time and space, this situation must not use the two-layer loop because the complexity of the n squared, so how to avoid another cycle. Further analysis of the problem, due to the time required to find two numbers in the sequence and equal to the target number, it is necessary to use a layer of loop, then you can think, in each loop to access the sequence element, we subtract the target number of the accessed element, and then find whether the value exists in the sequence, if it can be found, It's obvious that the value and the currently accessed element are the objects we're looking for. In this way, we have to store elements and their subscripts, and to have the find function, think of STL, we can use map<int,int> to match our idea, key is used to store the sequence element, value is used to store index, so the problem is solved. The detailed code is as follows:
/************************************************************************* > File Name:twoSum_hash.cc > Autho R:jerry Shi > Mail: [email protected] > Created time:2014 December 27 Saturday 14:52 27 sec. *********************** /#include <iostream> #include <vector> #include < iterator> #include <map> #include <set>using namespace Std;class solution{public:vector<int> Twosum (vector<int> &numbers,int target) {map<int,int> map;vector<int> result; Map<int,int>::iterator It;int i = 0;int other;for (i = 0; i < numbers.size (); ++i) {other = target-numbers.at (i); Std::map<int,int>::iterator it = Map.find (other); if (it = Map.end ()) {result.push_back (it->second+1);// Index2result.push_back (i+1); Index1}map.insert (pair<int,int> (numbers.at (i), i)); return result;}; int main (void) {vector<int> test; Vector<int> result;tesT.push_back (2); Test.push_back (7); Test.push_back (one); Test.push_back (); int target = 9; Solution Mtest;result = mtest.twosum (test,target); int i;for (i = 0; i < result.size (); i+=2) {cout << "index1 =" << result.at (i) << "\ T" << "index 2 = "<< result.at (i+1) << Endl;} return 0;}
Then submitted, passed the evaluation, time-consuming 112ms, and then see the topic solutions, said to be used hash, but also use the boost in the Unordermap, the general idea is the same.
Leetcode 1----twosum