2496. 数组中字符串的最大值
2496. 数组中字符串的最大值
The value of an alphanumeric string can be defined as:
- The numeric representation of the string in base
10
, if it comprises of digits only. - The length of the string, otherwise.
Given an array strs
of alphanumeric strings, return the maximum value of any string in strs
.
Example 1:
Input: strs = ["alic3","bob","3","4","00000"]
Output: 5
Explanation:
- "alic3" consists of both letters and digits, so its value is its length, i.e. 5.
- "bob" consists only of letters, so its value is also its length, i.e. 3.
- "3" consists only of digits, so its value is its numeric equivalent, i.e. 3.
- "4" also consists only of digits, so its value is 4.
- "00000" consists only of digits, so its value is 0.
Hence, the maximum value is 5, of "alic3".
Example 2:
Input: strs = ["1","01","001","0001"]
Output: 1
Explanation:
Each string in the array has value 1. Hence, we return 1.
Constraints:
1 <= strs.length <= 100
1 <= strs[i].length <= 9
strs[i]
consists of only lowercase English letters and digits.
做题
int maximumValue(vector<string>& strs) {
vector<int> ans;
ans.push_back(0);
for (string a: strs) {
bool flag = true;
for (char b: a) {
if ((b > '9' || b < '0')) {
ans.push_back(a.size());
flag = false;
}
}
if (flag == true) {
ans.push_back(std::atoi(a.c_str()));
}
}
std::sort(ans.begin(), ans.end(), greater<>{});
return ans[0];
}
2023/06/23 10:32:15
解答成功:
执行耗时:0 ms,击败了100.00% 的C++用户
内存消耗:7.5 MB,击败了59.83% 的C++用户
可能没有官解这么优雅,而且内存这么这么多啊。。
哦我知道了,官解用的引用。
2023/06/23 10:36:24
解答成功:
执行耗时:4 ms,击败了57.32% 的C++用户
内存消耗:7.7 MB,击败了15.48% 的C++用户
换引用怎么时间还加了,啊???内存也加了
玄学