在Github上开了一个关于leetcode的repository.
如果你也有兴趣可以把你的解法通过github上传,一起印证和学习.
https://github.com/LiLane/leetcode
例子1:
class Solution { public: /* Result: 26ms 时间复杂度:O(n) 空间复杂度:O(n) Author: Lane0x */ vector<int> twoSum(vector<int> &num,int target){ unordered_map<int,int> mapping; vector<int> result; for(int i = 0; i < num.size(); ++i) mapping[num[i]] = i; for(int i = 0; i < num.size(); ++i){ const int gap = target - num[i]; if(mapping.find(gap) != mapping.end() && mapping[gap] > i){ result.push_back(i + 1); result.push_back(mapping[gap] + 1); break; } } return result; } };
原文:http://blog.csdn.net/lane_l/article/details/45228091