LeetCode C++ 1694. Reformat Phone Number【String】简单
发布日期:2021-07-01 02:58:27 浏览次数:2 分类:技术文章

本文共 2941 字,大约阅读时间需要 9 分钟。

You are given a phone number as a string number. number consists of digits, spaces ' ', and/or dashes '-'.

You would like to reformat the phone number in a certain manner. Firstly, remove all spaces and dashes. Then, group the digits from left to right into blocks of length 3 until there are 4 or fewer digits. The final digits are then grouped as follows:

  • 2 digits: A single block of length 2.
  • 3 digits: A single block of length 3.
  • 4 digits: Two blocks of length 2 each.

The blocks are then joined by dashes. Notice that the reformatting process should never produce any blocks of length 1 and produce at most two blocks of length 2.

Return the phone number after formatting.

Example 1:

Input: number = "1-23-45 6"Output: "123-456"Explanation: The digits are "123456".Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".Step 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is "456".Joining the blocks gives "123-456".

Example 2:

Input: number = "123 4-567"Output: "123-45-67"Explanation: The digits are "1234567".Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".Step 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are "45" and "67".Joining the blocks gives "123-45-67".

Example 3:

Input: number = "123 4-5678"Output: "123-456-78"Explanation: The digits are "12345678".Step 1: The 1st block is "123".Step 2: The 2nd block is "456".Step 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is "78".Joining the blocks gives "123-456-78".

Example 4:

Input: number = "12"Output: "12"

Example 5:

Input: number = "--17-5 229 35-39475 "Output: "175-229-353-94-75"

Constraints:

  • 2 <= number.length <= 100
  • number consists of digits and the characters '-' and ' '.
  • There are at least two digits in number.

题意:给出一个字符串形式的电话号码 numbernumber 由数字、空格 ’ '、和破折号 ‘-’ 组成。

按下述方式重新格式化电话号码。

  • 首先,删除 所有的空格和破折号。
  • 其次,将数组从左到右 每 3 个一组 分块,直到 剩下 4 个或更少数字。剩下的数字将按下述规定再分块:
    • 2 个数字:分为单个含 2 个数字的块。
    • 3 个数字:分为单个含 3 个数字的块。
    • 4 个数字:分为两个分别含 2 个数字的块。

最后用破折号将这些块连接起来。注意,重新格式化过程中 不应该 生成仅含 1 个数字的块,并且 最多 生成两个含 2 个数字的块。返回格式化后的电话号码。


解法 顺序遍历

先删除掉所有的空格和破折号 spaces and dashes ,然后遍历整个字符串,根据剩下的数字字符的长度,决定使用哪一条规则:

class Solution {
public: string reformatNumber(string number) {
string s, ans; for (const char &c : number) if (isdigit(c)) s.push_back(c); int n = s.size(); for (int i = 0; i < n; ) {
if (n - i > 4) {
ans += s.substr(i, 3) + "-"; i += 3; } else if (n - i == 4) {
//剩下的分为两个块 ans += s.substr(i, 2) + "-" + s.substr(i + 2); break; } else if (n - i >= 2) {
//分为一个块 ans += s.substr(i); break; } } return ans; }};

提交后效率如下:

执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户内存消耗:6.3 MB, 在所有 C++ 提交中击败了85.81% 的用户

转载地址:https://memcpy0.blog.csdn.net/article/details/111601098 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode C++ 135. Candy【Greedy】困难
下一篇:LeetCode C++ 387. First Unique Character in a String【String/Hash Table】简单

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年05月01日 10时15分28秒