返回介绍

solution / 2400-2499 / 2496.Maximum Value of a String in an Array / README_EN

发布于 2024-06-17 01:03:04 字数 6360 浏览 0 评论 0 收藏 0

2496. Maximum Value of a String in an Array

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def maximumValue(self, strs: List[str]) -> int:
    def f(s: str) -> int:
      return int(s) if all(c.isdigit() for c in s) else len(s)

    return max(f(s) for s in strs)
class Solution {
  public int maximumValue(String[] strs) {
    int ans = 0;
    for (var s : strs) {
      ans = Math.max(ans, f(s));
    }
    return ans;
  }

  private int f(String s) {
    int x = 0;
    for (int i = 0, n = s.length(); i < n; ++i) {
      char c = s.charAt(i);
      if (Character.isLetter(c)) {
        return n;
      }
      x = x * 10 + (c - '0');
    }
    return x;
  }
}
class Solution {
public:
  int maximumValue(vector<string>& strs) {
    auto f = [](string& s) {
      int x = 0;
      for (char& c : s) {
        if (!isdigit(c)) {
          return (int) s.size();
        }
        x = x * 10 + c - '0';
      }
      return x;
    };
    int ans = 0;
    for (auto& s : strs) {
      ans = max(ans, f(s));
    }
    return ans;
  }
};
func maximumValue(strs []string) (ans int) {
  f := func(s string) (x int) {
    for _, c := range s {
      if c >= 'a' && c <= 'z' {
        return len(s)
      }
      x = x*10 + int(c-'0')
    }
    return
  }
  for _, s := range strs {
    if x := f(s); ans < x {
      ans = x
    }
  }
  return
}
function maximumValue(strs: string[]): number {
  const f = (s: string) => (Number.isNaN(Number(s)) ? s.length : Number(s));
  return Math.max(...strs.map(f));
}
impl Solution {
  pub fn maximum_value(strs: Vec<String>) -> i32 {
    let mut ans = 0;
    for s in strs.iter() {
      let num = s.parse().unwrap_or(s.len());
      ans = ans.max(num);
    }
    ans as i32
  }
}
public class Solution {
  public int MaximumValue(string[] strs) {
    return strs.Max(f);
  }

  private int f(string s) {
    int x = 0;
    foreach (var c in s) {
      if (c >= 'a') {
        return s.Length;
      }
      x = x * 10 + (c - '0');
    }
    return x;
  }
}
#define max(a, b) (((a) > (b)) ? (a) : (b))

int parseInt(char* s) {
  int n = strlen(s);
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (!isdigit(s[i])) {
      return n;
    }
    res = res * 10 + s[i] - '0';
  }
  return res;
}

int maximumValue(char** strs, int strsSize) {
  int ans = 0;
  for (int i = 0; i < strsSize; i++) {
    int num = parseInt(strs[i]);
    ans = max(ans, num);
  }
  return ans;
}

Solution 2

class Solution:
  def maximumValue(self, strs: List[str]) -> int:
    def f(s: str) -> int:
      x = 0
      for c in s:
        if c.isalpha():
          return len(s)
        x = x * 10 + ord(c) - ord("0")
      return x

    return max(f(s) for s in strs)
impl Solution {
  pub fn maximum_value(strs: Vec<String>) -> i32 {
    let parse = |s: String| -> i32 {
      let mut x = 0;

      for c in s.chars() {
        if c >= 'a' && c <= 'z' {
          x = s.len();
          break;
        }

        x = x * 10 + (((c as u8) - b'0') as usize);
      }

      x as i32
    };

    let mut ans = 0;
    for s in strs {
      let v = parse(s);
      if v > ans {
        ans = v;
      }
    }

    ans
  }
}

Solution 3

use std::cmp::max;

impl Solution {
  pub fn maximum_value(strs: Vec<String>) -> i32 {
    let mut ans = 0;

    for s in strs {
      match s.parse::<i32>() {
        Ok(v) => {
          ans = max(ans, v);
        }
        Err(_) => {
          ans = max(ans, s.len() as i32);
        }
      }
    }

    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文