数据量比较小,两个数相加最大不超过1000,那就先将1000以内的60的倍数存起来,
记录time数组中每个数是否出现,以及下标,
之后遍历time数组,对于time[i],遍历60的每个倍数m,如果m - time[i] 存在,并且下标大于i,则cnt++,存在几个就++几次,同时注意m 是否大于 time[i]
class Solution { public: vector<int> v; struct node{ int cnt = 0; vector<int> id; }Node[1010]; int numPairsDivisibleBy60(vector<int>& time) { for(int i = 60; i <= 1000; i += 60) v.push_back(i); int length = time.size(); int len = v.size(); for(int i = 0; i < length; i++) { Node[time[i]].cnt++; Node[time[i]].id.push_back(i); } int ans = 0; for(int i = 0; i < length; i++) { for(int j = 0; j < len; j++) { if(v[j] == time[i]) continue; else if(v[j] < time[i] || Node[v[j] - time[i]].cnt == 0) continue; else { int m = v[j] - time[i]; int len2 = Node[m].id.size(); for(int k = 0; k < len2; k++) if(Node[m].id[k] > i) ans++; } } } return ans; } };