1. Title, Sum
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
Given an array of integers, find out that two of them meet the sum equal to the target number you specified.
Requirement: This function Twosum must return an index that can add two numbers equal to the target number, and the index1 must be less than INDEX2. Note that the results of your return (including index1 and INDEX2) are not based on 0.
You can assume that each input must have only one result.
2. C + + Problem solving
2.1 Two-layer cycle, Time complexity O (n^2), Space complexity O (1)
Class Solution {public: vector<int> twosum (vector<int> &numbers, int target) { Vector<int > result; for (int i = 0; i < numbers.size ()-1; i++) {for (int j = i+1; J < Numbers.size (); j + +) { if (Numbers[i] + N Umbers[j]==target) { result.push_back (i+1); Result.push_back (j+1); return result;}} } return result; } };
2.2 Hash, time complexity O (n), Space complexity O (n)
Class Solution {public: vector<int> twosum (vector<int> &numbers, int target) { Vector<int > result; Map<int, int> m; if (Numbers.size () < 2) return result; for (int i = 0; i < numbers.size (); i++) m[numbers[i]] = i; Map<int, int>::iterator it; for (int i = 0; i < numbers.size (); i++) { if ((it = M.find (Target-numbers[i])! = M.end ()) { if (i = = It->second) continue; Result.push_back (i+1); Result.push_back (it->second+1); return result; } } return result;} ;
3. Python Problem Solving
Copy the original array deep, then sort--then find two numbers in the sorted array to add them to target: using two pointers, one pointing to the head, one pointing to the tail, two pointers moving in the middle and checking the sum of the number of two pointers and whether it is target. If you find these two numbers, and then the two numbers in the original array in the position to find out--one thing to note is: In the original array to find the subscript, you need a start-up, a tail to find, or can not pass. As this example: numbers=[0,1,2,0]; Target=0. If you start looking from scratch, you'll have a problem.
3.1
Class Solution:
# @return A tuple, (index1, Index2)
def twosum (self, num, target):
index = []
Numtosort = num[:]//Deep copy
Numtosort.sort ()
i = 0; j = Len (numtosort)-1
While I < j:
If Numtosort[i] + numtosort[j] = = target:
for k in range (0,len (num)):
If num[k] = = Numtosort[i]:
Index.append (k)
Break
for k in range (len (num) -1,-1,-1):
If num[k] = = Numtosort[j]:
Index.append (k)
Break
Index.sort ()
Break
Elif Numtosort[i] + Numtosort[j] < target:
i = i + 1
Elif Numtosort[i] + numtosort[j] > target:
j = J-1
Return (INDEX[0]+1,INDEX[1]+1)
3.2 Dictionaries
Class Solution:
# @return A tuple, (index1, Index2)
def twosum (self, num, target):
Dict = {}
For I in range (len (num)):
x = Num[i]
If target-x in Dict:
Return (dict[target-x]+1, i+1) //found, return two number of subscripts
DICT[X] = i
Leetcode--1