zoukankan      html  css  js  c++  java
  • 20210703第 237 场周赛(四)

    力扣第48场双周赛

    1796. 字符串中第二大的数字

    题目:

    给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。

    混合字符串 由小写英文字母和数字组成。

     

     题解:

    采用set里面的元素自动从小到大排序,而且没有重复元素。然后用两个整型变量找到第二大的数字,并输出即可。

    代码:

     1 class Solution {
     2 public:
     3     int secondHighest(string s) {
     4         set<int> sett;
     5         for(char ch:s)
     6         {
     7             if(isdigit(ch))
     8             {
     9                 sett.insert((ch-'0'));
    10             }
    11         }
    12         if(sett.size()<2) return -1;
    13         int x=0;
    14         int y=0;
    15         for(int i:sett)
    16         {
    17             if(i>x)
    18             {
    19                 x=i;
    20             }
    21             if(x>y)
    22             {
    23                 swap(x,y);
    24             }
    25         }
    26         return x; 
    27     }
    28 };

    1797. 设计一个验证系统

    题目:

    你需要设计一个包含验证码的验证系统。每一次验证中,用户会收到一个新的验证码,这个验证码在 currentTime 时刻之后 timeToLive 秒过期。如果验证码被更新了,那么它会在 currentTime (可能与之前的 currentTime 不同)时刻延长 timeToLive 秒。

    请你实现 AuthenticationManager 类:

    AuthenticationManager(int timeToLive) 构造 AuthenticationManager 并设置 timeToLive 参数。
    generate(string tokenId, int currentTime) 给定 tokenId ,在当前时间 currentTime 生成一个新的验证码。
    renew(string tokenId, int currentTime) 将给定 tokenId 且 未过期 的验证码在 currentTime 时刻更新。如果给定 tokenId 对应的验证码不存在或已过期,请你忽略该操作,不会有任何更新操作发生。
    countUnexpiredTokens(int currentTime) 请返回在给定 currentTime 时刻,未过期 的验证码数目。
    如果一个验证码在时刻 t 过期,且另一个操作恰好在时刻 t 发生(renew 或者 countUnexpiredTokens 操作),过期事件 优先于 其他操作。

    提示:

    1 <= timeToLive <= 108
    1 <= currentTime <= 108
    1 <= tokenId.length <= 5
    tokenId 只包含小写英文字母。
    所有 generate 函数的调用都会包含独一无二的 tokenId 值。
    所有函数调用中,currentTime 的值 严格递增 。
    所有函数的调用次数总共不超过 2000 次。

    思路:

    用map+暴力求解的。

    代码:

     1 class AuthenticationManager:
     2 
     3     def __init__(self, timeToLive: int):
     4         self.ttl = timeToLive
     5         self.map = {}
     6 
     7 
     8     def generate(self, tokenId: str, currentTime: int) -> None:
     9         self.map[tokenId] = currentTime + self.ttl
    10 
    11 
    12     def renew(self, tokenId: str, currentTime: int) -> None:
    13         if tokenId in self.map and self.map[tokenId] > currentTime:
    14             self.map[tokenId] = self.ttl + currentTime
    15 
    16 
    17     def countUnexpiredTokens(self, currentTime: int) -> int:
    18         return len([a for a in self.map if self.map[a] > currentTime])
    19 
    20 
    21 
    22 # Your AuthenticationManager object will be instantiated and called as such:
    23 # obj = AuthenticationManager(timeToLive)
    24 # obj.generate(tokenId,currentTime)
    25 # obj.renew(tokenId,currentTime)
    26 # param_3 = obj.countUnexpiredTokens(currentTime)

    1798. 你能构造出连续值的最大数目

    题目:

    给你一个长度为 n 的整数数组 coins ,它代表你拥有的 n 个硬币。第 i 个硬币的值为 coins[i] 。如果你从这些硬币中选出一部分硬币,它们的和为 x ,那么称,你可以 构造 出 x 。

    请返回从 0 开始(包括 0 ),你最多能 构造 出多少个连续整数。

    你可能有多个相同值的硬币。

    思路:

     代码:

    class Solution {
    public:
        int getMaximumConsecutive(vector<int>& coins) {
            sort(coins.begin(),coins.end());
            int a=0;

            for(int b:coins)
            {
                if(b<=a+1)
                {
                   a=a+b;
                }
                else
                {
                    break;
                }
            }
            return a+1;
        }
    };

    参考链接:

    https://leetcode-cn.com/problems/second-largest-digit-in-a-string/solution/c-gui-gui-zheng-zheng-shi-yong-set-by-ch-j3ic/

    https://leetcode-cn.com/problems/design-authentication-manager/solution/jun-tan-o1-shuang-xiang-lian-biao-ha-xi-c4igt/

    https://leetcode-cn.com/problems/maximum-number-of-consecutive-values-you-can-make/solution/ni-neng-gou-zao-chu-lian-xu-zhi-de-zui-d-hlxf/

    雪儿言
  • 相关阅读:
    c31 rotc_百度百科
    c31 rotc_百度百科
    希努尔男装股份有限公司_百度百科
    红领的短板:线下“O”瓶颈_财经频道_一财网
    持外地牌照在京车主每12天组团包车办进京证
    持外地牌照在京车主每12天组团包车办进京证
    关于我们-要约会
    nginx部署~dotnetCore+mvc网站502
    Rabbitmq~linux环境的部署
    Mongodb~Linux环境下的部署~服务的部署与自动化
  • 原文地址:https://www.cnblogs.com/weixq351/p/14967749.html
Copyright © 2011-2022 走看看