zoukankan      html  css  js  c++  java
  • 【leetcode】816. Ambiguous Coordinates

    题目如下:

     

    解题思路:我的方案是先把S拆分成整数对,例如S='1230',先拆分成(1,230),(12,30),(123,0),然后再对前面整数对进行加小数点处理。比如(12,30)中的12可以加上小数点可以变成(12)和(1.2),(30)可以变成(30)和(3.0)。接下来对加上小数点后的结果进行配对,可以得到(12,30),(12,3.0),(1.2,30),(1.2,3.0)四种,再过滤掉不合规则的(x,3.0),就可以得到结果。

    代码如下:

    class Solution(object):
        def is_number(self,s):
            import re
            if re.match('00.',s):
                return False
            if re.match('^0{2,}[1-9]*.?[0-9]*$',s):
                return False
            if re.match('^0+.0*$',s):
                return False
            if re.match('^0+[1-9]+.*[0-9]*$',s):
                return False
            if re.match('^[0-9]+.[0-9]*0$',s):
                return False
            if re.match('^0[0-9]+.?[0-9]*$',s):
                return False
            return True
        def ambiguousCoordinates(self, S):
            """
            :type S: str
            :rtype: List[str]
            """
            res = []
            S = S.replace('(','').replace(')','')
            queue = []
            for i in xrange(0,len(S)-1):
                queue.append((S[:i+1],S[i+1:]))
            while len(queue) > 0:
                x,y = queue.pop(0)
    
                xl = [x]
                yl = [y]
                for i in xrange(1,len(x)):
                    xl.append(x[:i] + '.' + x[i:])
                for i in xrange(1,len(y)):
                    yl.append(y[:i] + '.' + y[i:])
    
                for i in xl:
                    for j in yl:
                        if self.is_number(i) and self.is_number(j):
                            res.append('(' + str(i) + ', ' + str(j) + ')')
            return res
  • 相关阅读:
    软件体系结构课后作业03
    Struts2初步认识
    springboot1
    pytorch安装
    classifier of chains
    python错误集锦
    svm-惩罚因子
    毕设笔记1----div样式
    SQLite
    MVC实例应用模式
  • 原文地址:https://www.cnblogs.com/seyjs/p/8977966.html
Copyright © 2011-2022 走看看