Leetcode OJ The first question, if there is a problem or give me the advice welcome Letter Discussion ms08.shiroh@gmail.com topic description
The first problem of Leetcode OJ, the topic is described as follows:
Given an array of integers, find two numbers such this they add up to a specific target number.
The function twosum should return indices of the two numbers such this they add to the target,
Where index1 must be less than INDEX2. Please note that your returned answers (both Index1 and INDEX2)
are not zero-based.
You may assume this each input would have exactly one solution.
Given a sequence, if the sum of two elements exists for the given number target. Note that the returned two sequences require "not zero-based", that is, starting at 1 instead of starting with the 0 subscript as an array.
train of thought 1 first given sequence is unordered, if sequence (set has been small to large sort), then set a subscript a start from scratch, another subscript b from the end, if both and less than target, indicating that both and not big enough, Because B subscripts point to the maximum value of the sequence that is currently possible (since B begins to point to the maximum value, each reduction in B is due to B plus the lowest value of the sequence in the current scenario has exceeded target, so subscript A is incremented if the sum of both equals target, the desired result is obtained. If the sum of the two is greater than target, shows that the sum of the two is large, because a subscript points to the minimum value of the sequence currently possible (since a begins with a minimum value, and a increases each time because a plus the current maximum number of sequences is not already target), subscript b decreases. This method has only O (n) complexity at two subscript traversal, but O (NLGN) complexity at the time of the order, so the total complexity is O (NLGN). Idea 2 the bottleneck in the idea above is sorting, can you find the result directly without sorting? If there is a pair of solutions m, N, set m in the sequence of N in front. We iterate through the sequence and use data structure s to store the data that has been traversed. Traversing to M, looking for target-m whether in S, this time or not. But when we go to N, we find Target-n, which is M, in S, we get to know. The method has a time complexity of O (n), but may require additional O (n) storage space. The data structure to be very convenient to query, so the use of a dictionary (c + + in Map,python with dict, storage data and subscript), query complexity of O (1). Note The value returned is smaller than the previous one. Note that there are two elements in the same situation, such as 4,4,target=8. Do not return the same subscript code Python, O (NLGN)
def twosum (self, num, target):
Tmp_num = num[:]
tmp_num.sort ()
index1 = 0
index2 = Len (tmp_num)-1
While Index1 < index2:
tmp_target = tmp_num[index1] + tmp_num[index2]
if tmp_target = target: Break
Elif tmp_target > Target:
index2 = 1
Else:
index1 + = 1
If index1 = Index2: Return
( -1,-1)
else:
ans1 = Num.index (Tmp_num[index1])
ans2 = Num.index (tmp_num[index2))
if Ans2!= ans1:
# Not zero based return
(min (ans1, ans2) +1, Max (ans1, Ans2) +1)
else:
ans2 = Num[ans1+1:].index (tmp_num[ INDEX2]) return
(ans1+1, ans1+1+ans2+1)
Python,o (N)
def twoSum1 (self, num, target):
tmp_num = {} for
I in range (len (num)):
if Target-num[i] in Tmp_num:
# He Re do not need to deal with the condition i = target-i return
(tmp_num[target-num[i]]+1, i+1)
else:
tmp_num[nu M[i]] = I return
(-1,-1)
C++,o (N)
Class Solution {public
:
vector<int> twosum (vector<int> &numbers, int target) {
map< int, int> searched;
vector<int> Res;
for (int i = 0; i < numbers.size (); ++i) {
if (Searched.count (target-numbers[i)) {
Res.push_back (searched[t ARGET-NUMBERS[I]]+1);
Res.push_back (i+1);
return res;
} else {
Searched[numbers[i]] = i;
}
}
Res.push_back ( -1);
Res.push_back ( -1);
return res;
}
;