Leetcode 1796 寻找字符串中第二大的数字
1、两次遍历(省略)
2、一次遍历(max和second变量,代码省略)
3、范围0-9,桶排序(代码如下)
class Solution { public: int secondHighest(string s) { int sortArr[10] = {0}; for (auto ch : s) { int i = ch - '0'; if (i >= 0 && i <= 9) { sortArr[i]++; } } int sign = 0; for (int i = 9; i >= 0; i--) { if (sortArr[i] > 0) { sign++; if (sign == 2) { return i; } } } return -1; } };
标签:ch,int,1796,sign,字符串,Leetcode From: https://www.cnblogs.com/Damocles-Sword/p/16948967.html