LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

这篇具有很好参考价值的文章主要介绍了LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

一个由字母和数字组成的字符串的  定义如下:

  • 如果字符串  包含数字,那么值为该字符串在 10 进制下的所表示的数字。
  • 否则,值为字符串的 长度 。

给你一个字符串数组 strs ,每个字符串都只由字母和数字组成,请你返回 strs 中字符串的 最大值 。

示例 1:

输入:strs = ["alic3","bob","3","4","00000"]
输出:5
解释:
- "alic3" 包含字母和数字,所以值为长度 5- "bob" 只包含字母,所以值为长度 3- "3" 只包含数字,所以值为 3- "4" 只包含数字,所以值为 4- "00000" 只包含数字,所以值为 0 。
所以最大的值为 5 ,是字符串 "alic3" 的值。

示例 2:

输入:strs = ["1","01","001","0001"]
输出:1
解释:
数组中所有字符串的值都是 1 ,所以我们返回 1

提示:

  • 1 <= strs.length <= 100
  • 1 <= strs[i].length <= 9
  • strs[i] 只包含小写英文字母和数字。

解法 字符串

遍历输入数组中的字符串,判断字符串每一个字符是否都是数字。如果字符串只包含数字,那么转换该字符串为十进制下所表示的数字,否则值为字符串的长度。最后返回字符串数组里的最大值。

class Solution {
public:
    int maximumValue(vector<string>& strs) {
        int ans = 0; 
        for (auto &str : strs) {
            bool isDigits = true; 
            for (const char &c : str) isDigits &= isdigit(c);
            ans = max(ans, isDigits ? stoi(str) : (int)str.size());
        }
        return ans;
    }
};

复杂度分析:文章来源地址https://www.toymoban.com/news/detail-503151.html

  • 时间复杂度: O ( n m ) O(nm) O(nm) ,其中 n n n 是数组 s t r s strs strs 的长度, 其中 m m m 是字符串的长度。
  • 空间复杂度: O ( 1 ) O(1) O(1) ,不需要额外的空间。

到了这里,关于LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 833. Find And Replace in String【字符串,哈希表,模拟】1460

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月12日
    浏览(36)
  • java 字符串转数组(String to Array)

    java 字符串转数组(String to Array) 数组转List、 判断String数组中是否包含某个字符串

    2024年02月12日
    浏览(43)
  • List<HashMap<String,String>>实现自定义字符串排序(key排序、Value排序)

    SpringBoot+Vue3实现登录验证码功能 Java实现发送邮件(定时自动发送邮件) 换个角度使用Redis去解决跨域存取Session问题 Redis缓存穿透、击穿、雪崩问题及解决方法 Spring Cache的使用–快速上手篇 更多该系列文章请查看我的主页哦   根据一些真实需求遇到的问题,需进行排序后

    2024年02月07日
    浏览(65)
  • Python错题集-7:DeprecationWarning: Conversion of an array with ndim(被弃用警告)

    DeprecationWarning: Conversion of an array with ndim 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)   X[i] = np.random.normal(loc=Ex, scale=np.abs(Enn), size=1) DeprecationWarning: Conversion of an array with ndim  是一个警告,通常出

    2024年04月09日
    浏览(36)
  • LeetCode646. Maximum Length of Pair Chain——动态规划

    You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order. Example 1: Input: pairs = [[

    2024年02月22日
    浏览(38)
  • leetcode - 2616. Minimize the Maximum Difference of Pairs

    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents th

    2024年02月13日
    浏览(33)
  • 0052【Edabit ★☆☆☆☆☆】Learn Lodash: _.drop, Drop the First Elements of an Array

    0052【Edabit ★☆☆☆☆☆】Learn Lodash: _.drop, Drop the First Elements of an Array arrays Instructions According to the lodash documentation, _.drop creates a slice of an array with n elements dropped from the beginning. Your challenge is to write your own version using vanilla JavaScript. Examples Notes Do not attempt to import lodash; you are sim

    2024年02月06日
    浏览(43)
  • [VUE]报错: Invalid prop: type check failed for prop “value“. Expected String, Number, got Array found

    控制台报错:[Vue warn]: Invalid prop: type check failed for prop \\\"value\\\". Expected String, Number, got Array found in。 错误翻译:属性无效:属性“值”的类型检查失败。应为字符串,数字,在中找到了数组。  查找了报错相关文件,最终查出了问题所在,将数据类型写成了数组型。 将 改为 就

    2024年02月06日
    浏览(41)
  • LeetCode 1921. Eliminate Maximum Number of Monsters【贪心,计数排序】1527

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(42)
  • LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal.   Example 1: Input: root = [1,7,0,7,-8,null,null] Output: 2 **Explanation: ** Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return

    2024年01月23日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包