zoukankan      html  css  js  c++  java
  • 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.

    Approach #1: Brute Force. [Java] [Memory Limit Exceeded]

    class Solution {
        public List<String> ambiguousCoordinates(String S) {
            List<String> ans = new ArrayList<>();
            StringBuilder sb = new StringBuilder(S);
            for (int i = 1; i < S.length(); ++i) {
                StringBuilder perfix = new StringBuilder(sb.substring(0, i));
                StringBuilder suffix = new StringBuilder(sb.substring(i));
                if (valid(perfix) && valid(suffix)) {
                    List<String> l1 = split(perfix);
                    List<String> l2 = split(suffix);
    
                    for (int j = 0; j < l1.size(); ++j) {
                        for (int k = 0; k < l2.size(); ++k) {
                            String temp = "(" + l1.get(j) + ", " + l2.get(k) + ")";
                            ans.add(temp);
                        }
                    }
                }
            }
            return ans;
        }
        
        public List<String> split(StringBuilder sb) {
            List<String> ret = new ArrayList<>();
            if (sb.length() == 1) {
                ret.add(sb.toString());
                return ret;
            } else if (sb.charAt(0) == '0' && sb.charAt(1) == '0') {
                sb.insert(1, '.');
                ret.add(sb.toString());
                return ret;
            } else if (sb.charAt(sb.length() - 1) == '0' && sb.charAt(sb.length() - 2) == '0') {
                sb.insert(sb.length() - 1, '.');
                ret.add(sb.toString());
                return ret;
            } else {
                for (int i = 1; i < sb.length() - 1; ++i) {
                    StringBuilder temp = sb;
                    sb.insert(i, '.');
                    ret.add(sb.toString());
                }
            }
            return ret;
        }
        
        public boolean valid(StringBuilder sb) {
            if (sb.length() == 1) return true;
            if (sb.length() > 4 && sb.charAt(0) == '0' && sb.charAt(1) == 0 &&
                sb.charAt(sb.length() - 2) == '0' && sb.charAt(sb.length() - 1) == '0')
                return false;
            for (int i = 0; i < sb.length(); ++i) 
                if (sb.charAt(i) != '0') return true;
            return false;
        }
    }
    

      

    Approach #2: String. [Java]

    class Solution {
        public List<String> ambiguousCoordinates(String S) {
            List<String> ans = new ArrayList<>();
            int n = S.length();
            for (int i = 1; i < n - 1; ++i) {
                List<String> A = f(S.substring(1, i)), B = f(S.substring(i, n-1));
                for (String a : A) for (String b : B) ans.add("(" + a + ", " + b + ")");
            }
            return ans;
        }
        
        public List<String> f(String s) {
            int n = s.length();
            List<String> ret = new ArrayList<>();
            if (n == 0 || n > 1 && s.charAt(0) == '0' && s.charAt(n-1) == '0') return ret;
            if (n > 1 && s.charAt(0) == '0') {
                ret.add("0." + s.substring(1));
                return ret;
            }
            ret.add(s);
            if (n == 1 || s.charAt(n-1) == '0') return ret;
            for (int i = 1; i < n; ++i) {
                ret.add(s.substring(0, i) + "." + s.substring(i, n));
            }
            return ret;
        }
    }
    

    Analysis:

    if S == "" : return []

    if S == "0" : return [S]

    if S == "0XXXX0" : return []

    if S == "0XXXX" : return ["0.XXXX"]

    if S == "XXXX0" : return [S]

    return [S, "X.XXX", "XX.XX", "XXX.X" ...]

      

    Reference:

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

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    嵌入式工程师C语言面试常见的0x10个问题
    C语言初学者网站推荐
    strlen和sizeof
    基于Docker搭建GitLab和Maven私服
    linux暴露端口可以被外部访问
    MySQL新增用户及赋予权限
    Docker添加域名解析
    Netstat 网络命令详解
    Mysql索引太长导致同步数据结构失败解决方法
    完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10786871.html
Copyright © 2011-2022 走看看