Problem description
For a given array, find 2 numbers, they meet 2 numbers and equals a specific number, and return the index of these two numbers. (starting from 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 less 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, A, target=9
Output:index1=1, index2=2
Solution 1. Analysis of Ideas:
我们将求2个数的索引转换成在数组中搜索(target-nums[i])的位置;直观想,两层遍历,固定一个元素,循环右移另一个元素,逐个测试,时间复杂度$O(n^2)$,而$O(n^2)$一般不能接受,因此需要考虑改进算法:想法则是降低到$O(n\ lgn)$,最理想则是$O(n)$
2. Test examples:
正数、负数、0
3. Special Circumstances:
数组不足2个数、两数相加溢出、没有匹配的情况
4. Implement the implementation of the 4.1-tier traversal: O ( n 2 )
#include <iostream>using namespace STD;int* Twosum (int* Nums,intNumssize,intTargetint* returnsize) {returnsize = NULL;if(Numssize <2)returnReturnsize;inti =-1;intJ while(++i < numssize-1) {if(Nums[i] >= target)Continue; j = i; while(++j < Numssize) {compare_times++;if(Nums[i] + nums[j] = = target) Break; }if(j = = numssize)Continue;Else Break; }if(I! = numssize-1) {returnsize =New int[2]; returnsize[0] = i+1; returnsize[1] = j+1; }returnReturnsize;}intMain () {intNums[] = {-1,0,9,5,7, One, the, -};inttarget =9;int*index = NULL; index = Twosum (Nums,sizeof(nums)/sizeof(nums[0]), target, index);if(Index! = NULL)cout<<"index1 ="<<index[0]<<", Index2 ="<<index[1]<<endl; }
Submitted to Leetcode returns time Limit exceeded, which does not meet the timing requirements.
4.2 Sorting implementations O( n lgn)
First, the array is quickly sorted or inserted into a two-fork search tree, two-point lookup, when fixed an element nums[i], in the array to find Target-nums[i], this time to reduce the look to < Span class= "Mrow" id= "mathjax-span-20" > o ( l g &NBSP, n ) , the total consumption is O( n lgn)
4.3 Linear implementation-hash table O(n)
We know that the search for elements is the fastest O ( 1 ) , that is, directly indexed, Lenovo can only be a hash table or keyword index. The keyword index (from smallest to largest) consumes additional memory space.
Because C + + has a ready-made hash map, it uses C + + directly.
In addition, we require the index of the element, that is, the keyword of the hash table, so we use the array element as the key word of the hash table, and the index of the array element as the value of the hash TABLE element.
classsolution{ Public: vector<int>Twosum (Const vector<int>&nums,intTarget) { vector<int>Resultsif(Nums.size () <2) {returnResults } map <int , int>Hmap;//Insert to hash map for(inti =0; I < nums.size (); i++) {Hmap.insert (Pair <int,int> (nums[i], i));//element values do key values}intJ for(inti =0; I < nums.size (); i++) {//hmap.count (x): Number of occurrences of x in hash map if(Hmap.count (Target-nums[i])) {j = hmap[(Target-nums[i])];if(J < i) {Results.push_back (j+1); Results.push_back (i+1); } } }returnResults }};
Test
intMain () {intNums[] = {0,-3,2,7, One, the, -}; vector<int>Nums_v (Nums, nums+7);inttarget =9; while(1) {classSolution Sol; vector<int>Results = Sol.twosum (Nums_v, target);if(Results.size ())cout<<"index1 ="<<results[0]<<", Index2 ="<<results[1]<<endl; } getchar ();}
Leetcode 1: Find out two numbers added equals the given number of two sum