Leetcode | | The Sum of

Source: Internet
Author: User

In solving this problem, the first thought was to use two pointers, one in front, and the other followed. The trailing pointer then moves backwards until a matching element is found or the end of the array is reached. Continue with the next iteration. The code is as follows:

#include <iostream>#include <vector>using namespace STD;classSolution { Public: vector<int>Twosum ( vector<int>& Nums,intTarget) { vector<int>:: Iterator Iter1, iter2; vector<int>Result        Iter1 = Nums.begin (); Iter2 = Nums.begin (); for(; Iter1! = Nums.end ()-1;            iter1++) {result.clear (); Result.push_back ((Iter1-nums.begin ()) +1); for(Iter2 = Iter1 +1; Iter2! = Nums.end (); iter2++) {if(*iter1 + *iter2 = = target) {Result.push_back (Iter2-nums.begin ()) +1);returnResult }            }        }    }};intMain () { vector<int>Nums Nums.push_back (3); Nums.push_back (2); Nums.push_back (4); Nums.push_back ( the); Solution solution;cout<< Solution.twosum (Nums,7)[0] << Endl;cout<< Solution.twosum (Nums,7)[1] << Endl;  GetChar (); }

The result after submission is time limit exceed. Because the time efficiency of the algorithm is θ( n 2 ) , so the running time is too long.

The basic operation we do in the above code is to continuously compare whether the subsequent element is equal to a value, and convert the comparison operation to a lookup operation, that is, to find the value of the element that follows. For the map container, the lookup operation can be θ ( l g n ) Or θ(1) , so this will improve the time efficiency of the algorithm.

Useful Points of knowledge:
1. Convert the comparison operation into a find operation.
2. If you use the Map type container in C + + because she is using a tree at the bottom, the time efficiency of its lookup operation is θ ( l g n ) If you use the Unordered_map type container in C + + because she is using a hash table at the bottom, the time efficiency of its lookup operation is θ(1) ;

The code for using the map type container is as follows, with a run time of 24ms:

classSolution { Public: vector<int>Twosum ( vector<int>& Nums,intTarget) { map<int, int>Tree_map; vector<int>ResultintLength = Nums.size (); for(inti =0; i < length; i++) {if(Tree_map.count (Target-nums[i])) {Result.push_back (tree_map[target-nums[i]) +1); Result.push_back (i +1);returnResult        } Tree_map[nums[i]] = i; }    }};

The code for using the Unordered_map type container is as follows, with a run time of 16ms:

classSolution { Public: vector<int>Twosum ( vector<int>& Nums,intTarget) { unordered_map<int, int>Hash_map; vector<int>ResultintLength = Nums.size (); for(inti =0; i < length; i++) {if(Hash_map.count (Target-nums[i])) {Result.push_back (hash_map[target-nums[i]) +1); Result.push_back (i +1);returnResult        } Hash_map[nums[i]] = i; }    }};

Here is a reference article:
Refer to Article One
Reference Article Two

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode | | The Sum of

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.