Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode twoSum #1

Open
Karsonnel opened this issue Nov 1, 2016 · 0 comments
Open

LeetCode twoSum #1

Karsonnel opened this issue Nov 1, 2016 · 0 comments

Comments

@Karsonnel
Copy link
Owner

class Solution{
public:
vector twosum(vector& nums,int target)
{
int i, sum;
vector results;
map<int, int> hmap;
for(i=0; i<nums.size(); i++){
if(!hmap.count(nums[i])){
hmap.insert(pair<int, int>(nums[i], i));
}
if(hmap.count(target-nums[i])){
int n=hmap[target-nums[i]];
if(n<i){
results.push_back(n+1);
results.push_back(i+1);
//cout<<n+1<<", "<<i+1<<endl;
return results;
}

        }
    }
}

};
重点学习掌握了map 与vector 优化程序执行时间

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant