LeetCode 题解工作台

模糊坐标

我们有一些二维坐标,如 "(1, 3)" 或 "(2, 0.5)" ,然后我们移除所有逗号,小数点和空格,得到一个字符串 S 。返回所有可能的原始字符串到一个列表中。 原始的坐标表示法不会存在多余的零,所以不会出现类似于"00", "0.0", "0.00", "1.0", "001", "00.0…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 回溯·pruning

bolt

答案摘要

枚举纵坐标的起始位置,然后分别获取横、纵坐标的所有可能的表示形式,最后将横、纵坐标的所有可能的表示形式组合起来。 我们将一个坐标值 或 按照小数点的位置分成左右两部分,那么两部分应该满足以下条件:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 回溯·pruning 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

我们有一些二维坐标,如 "(1, 3)" 或 "(2, 0.5)",然后我们移除所有逗号,小数点和空格,得到一个字符串S。返回所有可能的原始字符串到一个列表中。

原始的坐标表示法不会存在多余的零,所以不会出现类似于"00", "0.0", "0.00", "1.0", "001", "00.01"或一些其他更小的数来表示坐标。此外,一个小数点前至少存在一个数,所以也不会出现“.1”形式的数字。

最后返回的列表可以是任意顺序的。而且注意返回的两个数字中间(逗号之后)都有一个空格。

 

示例 1:
输入: "(123)"
输出: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
示例 2:
输入: "(00011)"
输出:  ["(0.001, 1)", "(0, 0.011)"]
解释: 
0.0, 00, 0001 或 00.01 是不被允许的。
示例 3:
输入: "(0123)"
输出: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
示例 4:
输入: "(100)"
输出: [(10, 0)]
解释: 
1.0 是不被允许的。

 

提示:

  • 4 <= S.length <= 12.
  • S[0] = "(", S[S.length - 1] = ")", 且字符串 S 中的其他元素都是数字。

 

lightbulb

解题思路

方法一:暴力模拟

枚举纵坐标的起始位置,然后分别获取横、纵坐标的所有可能的表示形式,最后将横、纵坐标的所有可能的表示形式组合起来。

我们将一个坐标值 xxyy 按照小数点的位置分成左右两部分,那么两部分应该满足以下条件:

  1. 左半部分不能以 0 开头,除非左半部分只有 0;
  2. 右半部分不能以 0 结尾。

时间复杂度 O(n3)O(n^3),其中 nn 为字符串 SS 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def ambiguousCoordinates(self, s: str) -> List[str]:
        def f(i, j):
            res = []
            for k in range(1, j - i + 1):
                l, r = s[i : i + k], s[i + k : j]
                ok = (l == '0' or not l.startswith('0')) and not r.endswith('0')
                if ok:
                    res.append(l + ('.' if k < j - i else '') + r)
            return res

        n = len(s)
        return [
            f'({x}, {y})' for i in range(2, n - 1) for x in f(1, i) for y in f(i, n - 1)
        ]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate demonstrates a solid understanding of backtracking principles and pruning techniques.

  • question_mark

    The solution correctly handles string manipulation and checks for number validity during the backtracking process.

  • question_mark

    The candidate is able to optimize the search process, ensuring that invalid paths are pruned early.

warning

常见陷阱

外企场景
  • error

    Forgetting to prune invalid paths early, leading to unnecessary computation and inefficiency.

  • error

    Handling invalid cases like leading zeroes or misplaced decimal points improperly.

  • error

    Not considering edge cases where the number may be too long to fit in the coordinate format.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the input format by including additional constraints on the length of the coordinate components.

  • arrow_right_alt

    Increase the length of the string, forcing the backtracking solution to handle longer inputs and potentially explore more combinations.

  • arrow_right_alt

    Introduce a requirement where one of the coordinates must always contain a decimal point, adding an additional constraint to the search process.

help

常见问题

外企场景

模糊坐标题解:回溯·pruning | LeetCode #816 中等