https://leetcode.com/problems/two-sum/
Water problem, but since retired very few do the problem, really backward too strong, not consider the whole
Test instructions: Give an array, also a target, and ask which two numbers to add up to get target
Answer: Bucket row Orhash
1, note that the bucket sort, and the depth of the bucket is not necessarily 1, so hash[i] represents the number of I rather than is not present
2, because the subscript is involved, so be careful that the number of arrays can be fractions, my approach is to find min (X[i]), if min (X[i]) <0, then target+=-2*min (X[i]), x[i]+= ( -1) *min (x[i ]),
Very not accustomed to IS, leetcode OJ incredibly does not have a given number of range, this very headache, the array open how big ...
Class Solution {public:vector<int> Twosum (vector<int>& nums, int target) {int tmp,cnt=0; int num[100001],id[100001]; int t = 0; memset (num,0,sizeof (num)); Memset (id,0,sizeof (id)); for (int i=0;i<nums.size (); i++) {tmp = nums[i]; t = min (t,tmp); } cnt=0; if (t<0) {t=-t; for (int i=0;i<nums.size (); i++) {nums[i] + = t; int tmp= Nums[i]; NUM[TMP] + +; cnt++; id[tmp]=cnt; cout << nums[i] << Endl; } target + = 2*t; }else{for (int i=0;i<nums.size (); i++) {int tmp= nums[i]; NUM[TMP] + +; cnt++; id[tmp]=cnt; cout << nums[i] << Endl; }}//cout << "ttt=" << target << "<< t << endl int flag=0; vector<int>ans; for (int i=0;i<cnt;i++) {if (nums[i]<=target) {if (Num[target-nums[i]]) { if (target-nums[i] = = Nums[i] && num[nums[i]]<2) continue; flag = 1; Ans.push_back (i+1); Ans.push_back (id[target-nums[i]); Ans = i+id[Target-nums[i]]; Break }}} return ans; }};
Leetcode OJ 1Two Sum