题目大意如下:
Input: nums = [18,43,36,13,7]
Output: 54
数位之和相等的数字:
第一组
18 — 1 + 8 == 9 || 36 ---- 3 + 6 == 9
18 + 36 == 54
第二组
43 – 4 + 3 == 7 || 7自己
43 + 7 == 50
最终因为 54 > 50 所以输出 54
class Solution {
public:
int maximumSum(vector<int> &nums) {
int ans = -1;
int maxn[85]{}; // 数位最多81个 防止溢出
for (int num : nums) {
int sum = 0;
for(int x = num; x ; x /= 10) {
sum += x % 10;// 枚举位数和
}
if(maxn[sum]) { // 说明左边还有未枚举完成的 继续检查
ans = max(ans,maxn[sum] + num); // 更新
}
maxn[sum] = max(maxn[sum] , num); // 维护最大元素
}
return ans;
}
};