Skip to content

Commit 286e6a5

Browse files
committed
feat: leetcode 1796
1 parent 8afc690 commit 286e6a5

File tree

8 files changed

+117
-1
lines changed

8 files changed

+117
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- 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">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,12 @@
28312831
"path": "./problemset/check-if-one-string-swap-can-make-strings-equal/README.md",
28322832
"difficulty": "简单"
28332833
},
2834+
{
2835+
"id": "1796",
2836+
"title": "字符串中第二大的数字",
2837+
"path": "./problemset/second-largest-digit-in-a-string/README.md",
2838+
"difficulty": "简单"
2839+
},
28342840
{
28352841
"id": "1822",
28362842
"title": "数组元素积的符号",

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,16 @@
52395239
"url": "https://leetcode-cn.com/problems/find-center-of-star-graph/",
52405240
"path": "./problemset/find-center-of-star-graph/README.md"
52415241
},
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+
},
52425252
{
52435253
"id": "1800",
52445254
"title": {

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@
513513
| 1769. [移动所有球到每个盒子所需的最小操作数](../../problemset/minimum-number-of-operations-to-move-all-balls-to-each-box/README.md) | 中等 |
514514
| 1773. [统计匹配检索规则的物品数量](../../problemset/count-items-matching-a-rule/README.md) | 简单 |
515515
| 1790. [仅执行一次字符串交换能否使两个字符串相等](../../problemset/check-if-one-string-swap-can-make-strings-equal/README.md) | 简单 |
516+
| 1796. [字符串中第二大的数字](../../problemset/second-largest-digit-in-a-string/README.md) | 简单 |
516517
| 1822. [数组元素积的符号](../../problemset/sign-of-the-product-of-an-array/README.md) | 简单 |
517518
| 1958. [文件夹操作日志搜集器](../../problemset/crawler-log-folder/README.md) | 简单 |
518519
| 1961. [检查字符串是否为数组前缀](../../problemset/check-if-string-is-a-prefix-of-array/README.md) | 简单 |

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@
10481048

10491049
[1791. 找出星型图的中心节点](../../problemset/find-center-of-star-graph/README.md)
10501050

1051+
[1796. 字符串中第二大的数字](../../problemset/second-largest-digit-in-a-string/README.md)
1052+
10511053
[1800. 最大升序子数组和](../../problemset/maximum-ascending-subarray-sum/README.md)
10521054

10531055
[1822. 数组元素积的符号](../../problemset/sign-of-the-product-of-an-array/README.md)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)