LeetCode 题解工作台
重新格式化电话号码
给你一个字符串形式的电话号码 number 。 number 由数字、空格 ' ' 、和破折号 '-' 组成。 请你按下述方式重新格式化电话号码。 首先, 删除 所有的空格和破折号。 其次,将数组从左到右 每 3 个一组 分块, 直到 剩下 4 个或更少数字。剩下的数字将按下述规定再分块: 2 个数…
1
题型
6
代码语言
3
相关题
当前训练重点
简单 · String-driven solution strategy
答案摘要
我们先按照题意,去除字符串中的所有空格和破折号。 记当前字符串长度为 ,然后从前往后遍历字符串,每 个字符为一组,将其加入结果字符串中,共取 $n / 3$ 组。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路
题目描述
给你一个字符串形式的电话号码 number 。number 由数字、空格 ' '、和破折号 '-' 组成。
请你按下述方式重新格式化电话号码。
- 首先,删除 所有的空格和破折号。
- 其次,将数组从左到右 每 3 个一组 分块,直到 剩下 4 个或更少数字。剩下的数字将按下述规定再分块:
- 2 个数字:单个含 2 个数字的块。
- 3 个数字:单个含 3 个数字的块。
- 4 个数字:两个分别含 2 个数字的块。
最后用破折号将这些块连接起来。注意,重新格式化过程中 不应该 生成仅含 1 个数字的块,并且 最多 生成两个含 2 个数字的块。
返回格式化后的电话号码。
示例 1:
输入:number = "1-23-45 6" 输出:"123-456" 解释:数字是 "123456" 步骤 1:共有超过 4 个数字,所以先取 3 个数字分为一组。第 1 个块是 "123" 。 步骤 2:剩下 3 个数字,将它们放入单个含 3 个数字的块。第 2 个块是 "456" 。 连接这些块后得到 "123-456" 。
示例 2:
输入:number = "123 4-567" 输出:"123-45-67" 解释:数字是 "1234567". 步骤 1:共有超过 4 个数字,所以先取 3 个数字分为一组。第 1 个块是 "123" 。 步骤 2:剩下 4 个数字,所以将它们分成两个含 2 个数字的块。这 2 块分别是 "45" 和 "67" 。 连接这些块后得到 "123-45-67" 。
示例 3:
输入:number = "123 4-5678" 输出:"123-456-78" 解释:数字是 "12345678" 。 步骤 1:第 1 个块 "123" 。 步骤 2:第 2 个块 "456" 。 步骤 3:剩下 2 个数字,将它们放入单个含 2 个数字的块。第 3 个块是 "78" 。 连接这些块后得到 "123-456-78" 。
示例 4:
输入:number = "12" 输出:"12"
示例 5:
输入:number = "--17-5 229 35-39475 " 输出:"175-229-353-94-75"
提示:
2 <= number.length <= 100number由数字和字符'-'及' '组成。number中至少含 2 个数字。
解题思路
方法一:简单模拟
我们先按照题意,去除字符串中的所有空格和破折号。
记当前字符串长度为 ,然后从前往后遍历字符串,每 个字符为一组,将其加入结果字符串中,共取 组。
若最后剩余 个字符,则将前面的最后一组的最后一个字符与该字符组成一个新的两个字符的组,加入结果字符串中;若最后剩余 个字符,直接将这连个字符组成一个新的组,加入结果字符串中。
最后将所有组之间加上破折号,返回结果字符串即可。
时间复杂度 ,空间复杂度 。其中 为字符串长度。
class Solution:
def reformatNumber(self, number: str) -> str:
number = number.replace("-", "").replace(" ", "")
n = len(number)
ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]
if n % 3 == 1:
ans[-1] = ans[-1][:2]
ans.append(number[-2:])
elif n % 3 == 2:
ans.append(number[-2:])
return "-".join(ans)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Looking for a candidate to demonstrate knowledge of string manipulation techniques.
- question_mark
Testing the candidate's ability to break down a problem into manageable steps for digit grouping and reformatting.
- question_mark
Evaluating the candidate’s understanding of edge cases, such as handling blocks of length 1 or 2.
常见陷阱
外企场景- error
Forgetting to remove non-digit characters first, leading to incorrect results or errors in grouping.
- error
Improperly handling the final groupings, resulting in invalid block sizes (e.g., a block of length 1).
- error
Failing to account for the possibility of having exactly 4 digits left, which must be split into two blocks of 2 digits.
进阶变体
外企场景- arrow_right_alt
Handling phone numbers with special characters other than dashes and spaces.
- arrow_right_alt
Reformatting a phone number where the final block consists of exactly 1 digit.
- arrow_right_alt
Adapting the solution to different phone number formats, such as country codes.