Q2342.h 701 B

12345678910111213141516171819202122232425262728
  1. #ifndef LEECODE_C_Q2342_H
  2. #define LEECODE_C_Q2342_H
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. int maximumSum(vector<int> &nums)
  7. {
  8. int ans = -1;
  9. int mx[82]{}; // 至多 9 个 9 相加
  10. for (int num : nums)
  11. {
  12. int s = 0; // num 的数位和
  13. for (int x = num; x; x /= 10)
  14. { // 枚举 num 的每个数位
  15. s += x % 10;
  16. }
  17. if (mx[s])
  18. { // 说明左边也有数位和等于 s 的元素
  19. ans = max(ans, mx[s] + num); // 更新答案的最大值
  20. }
  21. mx[s] = max(mx[s], num); // 维护数位和等于 s 的最大元素
  22. }
  23. return ans;
  24. }
  25. #endif LEECODE_C_Q2342_H
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。