File tree Expand file tree Collapse file tree 8 files changed +117
-1
lines changed
problemset/second-largest-digit-in-a-string Expand file tree Collapse file tree 8 files changed +117
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
<p align =" center " >
4
4
<!-- TOPICS COUNT START -->
5
- <img src =" https://img.shields.io/badge/-进度:560 -green " alt =" 进度:560 " >
5
+ <img src =" https://img.shields.io/badge/-进度:561 -green " alt =" 进度:561 " >
6
6
<!-- TOPICS COUNT END -->
7
7
<a href =" ./assets/docs/TOPICS.md " ><img src =" https://img.shields.io/badge/-题库目录-blue " alt =" 题库目录 " ></a >
8
8
<a href =" ./assets/docs/CATEGORIES.md " ><img src =" https://img.shields.io/badge/-题库分类-red " alt =" 题库分类 " ></a >
Original file line number Diff line number Diff line change 2831
2831
"path" : " ./problemset/check-if-one-string-swap-can-make-strings-equal/README.md" ,
2832
2832
"difficulty" : " 简单"
2833
2833
},
2834
+ {
2835
+ "id" : " 1796" ,
2836
+ "title" : " 字符串中第二大的数字" ,
2837
+ "path" : " ./problemset/second-largest-digit-in-a-string/README.md" ,
2838
+ "difficulty" : " 简单"
2839
+ },
2834
2840
{
2835
2841
"id" : " 1822" ,
2836
2842
"title" : " 数组元素积的符号" ,
Original file line number Diff line number Diff line change 5239
5239
"url" : " https://leetcode-cn.com/problems/find-center-of-star-graph/" ,
5240
5240
"path" : " ./problemset/find-center-of-star-graph/README.md"
5241
5241
},
5242
+ {
5243
+ "id" : " 1796" ,
5244
+ "title" : {
5245
+ "cn" : " 字符串中第二大的数字" ,
5246
+ "en" : " second-largest-digit-in-a-string"
5247
+ },
5248
+ "difficulty" : " 简单" ,
5249
+ "url" : " https://leetcode.cn/problems/second-largest-digit-in-a-string/" ,
5250
+ "path" : " ./problemset/second-largest-digit-in-a-string/README.md"
5251
+ },
5242
5252
{
5243
5253
"id" : " 1800" ,
5244
5254
"title" : {
Original file line number Diff line number Diff line change 513
513
| 1769. [ 移动所有球到每个盒子所需的最小操作数] ( ../../problemset/minimum-number-of-operations-to-move-all-balls-to-each-box/README.md ) | 中等 |
514
514
| 1773. [ 统计匹配检索规则的物品数量] ( ../../problemset/count-items-matching-a-rule/README.md ) | 简单 |
515
515
| 1790. [ 仅执行一次字符串交换能否使两个字符串相等] ( ../../problemset/check-if-one-string-swap-can-make-strings-equal/README.md ) | 简单 |
516
+ | 1796. [ 字符串中第二大的数字] ( ../../problemset/second-largest-digit-in-a-string/README.md ) | 简单 |
516
517
| 1822. [ 数组元素积的符号] ( ../../problemset/sign-of-the-product-of-an-array/README.md ) | 简单 |
517
518
| 1958. [ 文件夹操作日志搜集器] ( ../../problemset/crawler-log-folder/README.md ) | 简单 |
518
519
| 1961. [ 检查字符串是否为数组前缀] ( ../../problemset/check-if-string-is-a-prefix-of-array/README.md ) | 简单 |
Original file line number Diff line number Diff line change 1048
1048
1049
1049
[ 1791. 找出星型图的中心节点] ( ../../problemset/find-center-of-star-graph/README.md )
1050
1050
1051
+ [ 1796. 字符串中第二大的数字] ( ../../problemset/second-largest-digit-in-a-string/README.md )
1052
+
1051
1053
[ 1800. 最大升序子数组和] ( ../../problemset/maximum-ascending-subarray-sum/README.md )
1052
1054
1053
1055
[ 1822. 数组元素积的符号] ( ../../problemset/sign-of-the-product-of-an-array/README.md )
Original file line number Diff line number Diff line change
1
+ # 字符串中第二大的数字
2
+
3
+ > 难度:简单
4
+ >
5
+ > https://leetcode.cn/problems/second-largest-digit-in-a-string/
6
+
7
+ ## 题目
8
+
9
+ 给你一个混合字符串 ` s ` ,请你返回 ` s ` 中 ** 第二大** 的数字,如果不存在第二大的数字,请你返回 ` -1 ` 。
10
+
11
+ ** 混合字符串** 由小写英文字母和数字组成。
12
+
13
+ ### 示例
14
+
15
+ #### 示例 1:
16
+
17
+ ```
18
+ 输入:s = "dfa12321afd"
19
+ 输出:2
20
+ 解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。
21
+ ```
22
+
23
+ #### 示例 2:
24
+
25
+ ```
26
+ 输入:s = "abc1111"
27
+ 输出:-1
28
+ 解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。
29
+ ```
30
+
31
+ ## 解题
32
+
33
+ ``` ts
34
+ /**
35
+ * 直接遍历
36
+ * @desc 时间复杂度 O(n) 空间复杂度 O(1)
37
+ * @param s
38
+ * @returns
39
+ */
40
+ export function secondHighest(s : string ): number {
41
+ let first = - 1
42
+ let second = - 1
43
+ for (let i = 0 ; i < s .length ; i ++ ) {
44
+ const c = s [i ]
45
+ if (c >= ' 0' && c <= ' 9' ) {
46
+ const num = c .charCodeAt (0 ) - ' 0' .charCodeAt (0 )
47
+ if (num > first ) {
48
+ second = first
49
+ first = num
50
+ }
51
+ else if (num < first && num > second ) {
52
+ second = num
53
+ }
54
+ }
55
+ }
56
+ return second
57
+ }
58
+ ```
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { secondHighest } from '.'
3
+
4
+ describe ( '字符串中第二大的数字' , ( ) => {
5
+ testCase ( secondHighest )
6
+ } )
7
+
8
+ function testCase ( fn : ( s : string ) => number ) {
9
+ it . each ( [
10
+ [ 'dfa12321afd' , 2 ] ,
11
+ [ 'abc1111' , - 1 ] ,
12
+ ] ) ( '示例%#' , ( s , expected ) => {
13
+ expect ( fn ( s ) ) . toBe ( expected )
14
+ } )
15
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 直接遍历
3
+ * @desc 时间复杂度 O(n) 空间复杂度 O(1)
4
+ * @param s
5
+ * @returns
6
+ */
7
+ export function secondHighest ( s : string ) : number {
8
+ let first = - 1
9
+ let second = - 1
10
+ for ( let i = 0 ; i < s . length ; i ++ ) {
11
+ const c = s [ i ]
12
+ if ( c >= '0' && c <= '9' ) {
13
+ const num = c . charCodeAt ( 0 ) - '0' . charCodeAt ( 0 )
14
+ if ( num > first ) {
15
+ second = first
16
+ first = num
17
+ }
18
+ else if ( num < first && num > second ) {
19
+ second = num
20
+ }
21
+ }
22
+ }
23
+ return second
24
+ }
You can’t perform that action at this time.
0 commit comments