classSolution { public: typedeflonglong ll; intmaxLength(vector<int>& nums){ ll n = nums.size(), ans = 1; for(int i = 0; i < n ; i++){ ll a = nums[i], b = nums[i], c = nums[i]; ll t = i; for(int j = i + 1; j < n; j++) { if(a > INT_MAX) break; a = a * nums[j]; b = gcd(b, nums[j]); c = lcm(c, nums[j]); if(a == (b * c)) { t = j; } } ans = max(ans, t - i + 1); } return ans; } };
Q2 计算字符串的镜像分数
用一个大小为26的数组,每个元素是一个栈,遍历模拟一边即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: longlongcalculateScore(string s){ longlong ans = 0; stack<int> q[26]; int n = s.size(); for(int i = 0; i < n; i++) { if(q[25 - (s[i] - 'a')].size()){ ans += (i - q[25 - (s[i] - 'a')].top()); q[25 - (s[i] - 'a')].pop(); } else q[s[i] - 'a'].push(i); } return ans; } };