【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

 

转载于:https://www.cnblogs.com/seyjs/p/8977966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值