2496.数组中字符串的最大值
链接:2496.数组中字符串的最大值
难度:Easy
标签:数组、字符串
简介:给你一个字符串数组 strs ,每个字符串都只由字母和数字组成,请你返回 strs 中字符串的 最大值 。
题解 1 - cpp
- 编辑时间:2023-06-23
- 内存消耗:7.7MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
    int maximumValue(vector<string>& strs) {
        int res = 0;
        for (auto &s : strs) {
            int cur = 0;
            for (auto &c : s) {
                if (!isdigit(c)) {
                    cur = s.size();
                    break;
                } else {
                    cur = cur * 10 + c - '0';
                }
            }
            res = max(res, cur);
        }
        return res;
    }
};
题解 2 - rust
- 编辑时间:2023-06-23
- 内存消耗:2MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
    pub fn maximum_value(strs: Vec<String>) -> i32 {
        strs.into_iter()
            .map(|s| s.parse().unwrap_or(s.len() as i32))
            .max()
            .unwrap()
    }
}
题解 3 - python
- 编辑时间:2023-06-23
- 执行用时:32ms
- 内存消耗:16.1MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
    def maximumValue(self, strs: List[str]) -> int:
        return max(
            len(s) if not s.isdigit() else int(s)
            for s in strs
        )