[Leetcode] 816. 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.

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".

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.)

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:

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

思路

这是一道字符串处理的题目,主要考查字符串表示为合法整数或者小数的问题。题目不难,但是需要处理各种特殊情况。

我们的思路是:首先找出x坐标和y坐标的分界点。而x坐标和y坐标的字符串不可能为“00”之类,所以在分割的时候就排除掉这种情况。对于每个分割出来的字符串,我们想办法将其分割为整数部分和小数部分。整数部分不能以‘0’开始(整数0除外);小数部分则不能以‘0’结尾。最后把x坐标的所有可能值和y坐标的所有可能值组合一下就可以了。

代码

class Solution {
public:
    vector<string> ambiguousCoordinates(string S) {
        S = S.substr(1, S.length() - 2);
        vector<string> ret;
        for (int i = 1; i < S.length(); ++i) {
            string s1 = S.substr(0, i);
            string s2 = S.substr(i);
            if ((s1.length() > 1 && stoi(s1) == 0) || (s2.length() > 1 && stoi(s2) == 0)) {
                continue;   // make sure no "000" appears
            }
            vector<string> integers = getValues(s1);
            vector<string> fractors = getValues(s2);
            for (int i = 0; i < integers.size(); ++i) {
                for (int j = 0; j < fractors.size(); ++j) {
                    ret.push_back("(" + integers[i] + ", " + fractors[j] + ")");
                }
            }
        }
        return ret;
    }
private:
    vector<string> getValues(const string &s) {
        vector<string> ret;
        if (s.length() == 1 || s[0] != '0') {                       // avoid the case "01"
            ret.push_back(s);
        }
        for (int i = 1; i < s.length(); ++i) {
            string integer = s.substr(0, i);
            string fractor = s.substr(i);
            if (integer.length() > 1 && integer[0] == '0') {        // avoid the case "01"
                continue;
            }
            if (stoi(fractor) == 0 || fractor.back() == '0') {      // avoid the case "0.0" and "0.10"
                continue;
            }
            ret.push_back(integer + "." + fractor);
        }
        return ret;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值