[LeetCode] Ambiguous Coordinates 模糊的座標

 

We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we removed all commas, decimal points, and spaces, and ended up with the string S.  Return a list of strings representing all possibilities for what our original coordinates could have been.html

Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with less digits.  Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like ".1".git

The final answer list can be returned in any order.  Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.)less

Example 1:
Input: "(123)"
Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
Example 2:
Input: "(00011)"
Output:  ["(0.001, 1)", "(0, 0.011)"]
Explanation: 
0.0, 00, 0001 or 00.01 are not allowed.
Example 3:
Input: "(0123)"
Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
Example 4:
Input: "(100)"
Output: [(10, 0)]
Explanation: 
1.0 is not allowed.

 

Note:spa

  • 4 <= S.length <= 12.
  • S[0] = "(", S[S.length - 1] = ")", and the other elements in S are digits.

 

這道題給了咱們一個模糊座標,括號裏面很只有一個數字字符串,沒有逗號也沒有小數點,讓咱們本身添加逗號和小數點,讓把全部可能的組合都返回。題目中給了不少例子,理解起題意來很容易。這道題的難點是如何合理的拆分,不少拆分是不合法的,題目舉了不少不合法的例子,好比 "00", "0.0", "0.00", "1.0", "001", "00.01"。那麼咱們須要概括出全部不合法的corner case,而後剩下通常狀況好比123,咱們就按位加小數點便可。那麼咱們再來看一下那些非法的例子,咱們發現一眼望去好多0,不錯,0就是trouble maker,首先不能有0開頭的長度大於1的整數,好比00, 001。其次,不能有0結尾的小數,好比0.0,0.00,1.0等。還有,小數的整數位上也不能有0開頭的長度大於1的整數。那麼咱們來概括一下吧,首先若是字符串爲空,那麼直接返回空集合。而後若是字符串長度大於1,且首尾字符都是0的話,那麼不可分,好比 0xxx0,由於整數長度大於1的話不能以0開頭,中間也無法加小數點,由於小數最後一位不能是0。若是長度大於1,第一位是0,但最後一位不是0,那咱們能夠在第一個0後面加個小數點返回,這時就必需要加小數點了,由於長度大於1的整數不能以0開頭。再以後,若是最後一位是0,說明不能加小數點,直接把當前值返回便可。最後就是通常狀況了,咱們先把原數加入結果res,而後遍歷中間的每一個位置,都加個小數點,全部狀況概括以下:code

if S == "": return []
if S == "0": return [S]
if S == "0XXX0": return []
if S == "0XXX": return ["0.XXX"]
if S == "XXX0": return [S]
return [S, "X.XXX", "XX.XX", "XXX.X"...]htm

 

class Solution {
public:
    vector<string> ambiguousCoordinates(string S) {
        vector<string> res;
        int n = S.size();
        for (int i = 1; i < n - 2; ++i) {
            vector<string> A = findAll(S.substr(1, i)), B = findAll(S.substr(i + 1, n - 2 - i));
            for (auto &a : A) {
                for (auto &b : B) {
                    res.push_back("(" + a + ", " + b + ")");
                }
            }
        }
        return res;
    }
    vector<string> findAll(string S) {
        int n = S.size();
        if (n == 0 || (n > 1 && S[0] == '0' && S[n - 1] == '0')) return {};
        if (n > 1 && S[0] == '0') return {"0." + S.substr(1)};
        if (S[n - 1] == '0') return {S};
        vector<string> res{S};
        for (int i = 1; i < n; ++i) res.push_back(S.substr(0, i) + "." + S.substr(i));
        return res;
    }
};

 

參考資料:blog

https://leetcode.com/problems/ambiguous-coordinates/ci

https://leetcode.com/problems/ambiguous-coordinates/discuss/123851/C%2B%2BJavaPython-Solution-with-Explanationelement

 

LeetCode All in One 題目講解彙總(持續更新中...)leetcode

相關文章
相關標籤/搜索