剑指 Offer II 006. 排序数组中两个数字之和
水题,vis记录,暴力即可
注意有重复
class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int len = numbers.size(); int vis[3001]; memset(vis, 0, sizeof(vis)); for(int i = 0; i < len; i++) { if(numbers[i] < 0) { int temp = abs(numbers[i]); if(vis[temp] == 0) vis[temp] = 2; else if(vis[temp] == 1) vis[temp] = 3; else if(vis[temp] == 2) vis[temp] = 4; } else { int temp = numbers[i]; if(vis[temp] == 0) vis[temp] = 1; else if(vis[temp] == 2) vis[temp] = 3; else if(vis[temp] == 1) vis[temp] = 5; } } vector<int> ret; int flag = 0, k; for(int i = 0; i < len; i++) { int d = target - numbers[i]; if(d == numbers[i]) { if(d < 0 && vis[-d] == 4) ret.push_back(i); if(d >= 0 && vis[d] == 5) ret.push_back(i); } else if(d < 0) { d = -d; if(vis[d] == 2 || vis[d] == 3) { ret.push_back(i); } } else { if(vis[d] == 1 || vis[d] == 3) { ret.push_back(i); } } } return ret; } };
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。