mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-01-13 02:38:37 +08:00
init
This commit is contained in:
28
23/11/Q2342.h
Normal file
28
23/11/Q2342.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef LEECODE_C_Q2342_H
|
||||
#define LEECODE_C_Q2342_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
int maximumSum(vector<int> &nums)
|
||||
{
|
||||
int ans = -1;
|
||||
int mx[82]{}; // 至多 9 个 9 相加
|
||||
for (int num : nums)
|
||||
{
|
||||
int s = 0; // num 的数位和
|
||||
for (int x = num; x; x /= 10)
|
||||
{ // 枚举 num 的每个数位
|
||||
s += x % 10;
|
||||
}
|
||||
if (mx[s])
|
||||
{ // 说明左边也有数位和等于 s 的元素
|
||||
ans = max(ans, mx[s] + num); // 更新答案的最大值
|
||||
}
|
||||
mx[s] = max(mx[s], num); // 维护数位和等于 s 的最大元素
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
#endif LEECODE_C_Q2342_H
|
||||
Reference in New Issue
Block a user