File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ <h1 style =" text-align : center ;" > <span style =" color : #00AF9B ;" >3174. 清除数字</span > </h1 >
2+
3+ ### 🚀 LeetCode
4+
5+ <base target =" _blank " >
6+
7+ <span style =" color : #00AF9B ;" >** Easy** </span > [ ** https://leetcode.cn/problems/clear-digits/ ** ] ( https://leetcode.cn/problems/clear-digits/ )
8+
9+ ---
10+
11+ ### ❓ Description
12+
13+ <br />
14+
15+ 给你一个字符串 ` s ` 。
16+
17+ 你的任务是重复以下操作删除 ** 所有** 数字字符:
18+
19+ * 删除 ** 第一个数字字符** 以及它左边 ** 最近** 的 ** 非数字** 字符。
20+
21+ 请你返回删除所有数字字符以后剩下的字符串。
22+
23+ <br />
24+
25+ ** 示例 1:**
26+
27+ ```
28+ 输入: s = "abc"
29+ 输出: "abc"
30+ 解释: 字符串中没有数字
31+ ```
32+
33+ ** 示例 2:**
34+
35+ ```
36+ 输入: s = "cb34"
37+ 输出: ""
38+ 解释: 一开始, 我们对 s[2] 执行操作, s 变为 "c4"; 然后对 s[1] 执行操作, s 变为 ""
39+ ```
40+
41+ <br />
42+
43+ ** 提示:**
44+
45+ * ` 1 <= s.length <= 100 `
46+ * ` s ` 只包含小写英文字母和数字字符
47+ * 输入保证所有数字都可以按以上操作被删除
48+
49+ ---
50+
51+ ### ❗ Solution
52+
53+ <br />
54+
55+ #### Java
56+
57+ ```
58+ class Solution {
59+ public String clearDigits(String s) {
60+ StringBuilder result = new StringBuilder();
61+ for (char c : s.toCharArray()) {
62+ if (Character.isDigit(c)) {
63+ result.deleteCharAt(result.length() - 1);
64+ } else {
65+ result.append(c);
66+ }
67+ }
68+ return result.toString();
69+ }
70+ }
71+ ```
Original file line number Diff line number Diff line change 9393| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 2235.两整数相加** ] ( ../easy/2235.两整数相加.md ) |
9494| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 2236.判断根结点是否等于子结点之和** ] ( ../easy/2236.判断根结点是否等于子结点之和.md ) |
9595| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 2980.检查按位或是否存在尾随零** ] ( ../easy/2980.检查按位或是否存在尾随零.md ) |
96+ | <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 3174.清除数字** ] ( ../easy/3174.清除数字.md ) |
9697| <span style =" color : #FFB822 ;" >** Medium** </span > | [ ** LCR016.无重复字符的最长子串** ] ( ../medium/LCR016.无重复字符的最长子串.md ) |
9798| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** LCR024.反转链表** ] ( ../easy/LCR024.反转链表.md ) |
9899| <span style =" color : #FF2D55 ;" >** Hard** </span > | [ ** LCR051.二叉树中的最大路径和** ] ( ../hard/LCR051.二叉树中的最大路径和.md ) |
You can’t perform that action at this time.
0 commit comments