Leetcode OJ 1Two Sum

Source: Internet
Author: User

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

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.