题目如下:

解题思路:我的方案是先把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

【leetcode】816. Ambiguous Coordinates的更多相关文章

  1. 【LeetCode】816. Ambiguous Coordinates 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/ambiguous ...

  2. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  3. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. Java实体类之间的映射(多对多关系)

    多对对的映射,可以用学生和课程进行演示.一个学生可以选择多个课程,一个课程又对应了多个学生 定义学生类 class Stu{ private String name; private String n ...

  2. master-slave replication

    redis save 备份 恢复 root@ubuntu:/etc/init.d# find / -name dump.rdb |xargs ls -alt redis-cli save cp /va ...

  3. SQL*Plus 与数据库的交互

    设置SQL *Plus的运行环境 SET 命令格式: set system_variable value pagesize :从顶部标题到页结束之间的行数 默认是14 newpage:一页中空行的数量 ...

  4. Delphi XE2 之 FireMonkey 入门(11) - 控件居中、旋转、透明

    RotationAngle.RotationCenter.Opacity 属性继承自 TControl(FMX.Types), 这些新属性成了控件的基本功能. 先在 HD 窗体上添加 TRectang ...

  5. Delphi XE2 之 FireMonkey 入门(2)

    FireMonkey 的控件都是自己绘制的(而不是基于系统组件), 我想它们应该是基于一些基本图形; 就从基本图形开始吧. FMX.Objects 单元给出的类: TShape //基本图形的基类 T ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_2_字符串的构造方法和直接创建

    string的构造方法 psvm创建main方法 把字节翻译成了小a小b小c.字符串的底层科室用的byte字节数组 Ctrl+鼠标左键点击string 这个byte就保存了字符串底层的字节数据 直接创 ...

  7. 前端 CSS 优先级 样式设置important

    !important 的使用. !important方式来强制让样式生效,但并不推荐使用.因为如果过多的使用!important会使样式文件混乱不易维护. 万不得已可以使用!important 现在选 ...

  8. select,poll 和 epoll ??

    其实所有的 I/O 都是轮询的方法,只不过实现的层面不同罢了. 其中 tornado 使用的就是 epoll 的. selec,poll 和 epoll 区别总结 基本上 select 有 3 个缺点 ...

  9. 除了a链接跳转,还有其他的跳转方式

    一.直接在要跳转部分加上onclick事件 1.加入onclick事件: <div onclick="window.open('http://baidu.com','_blank')& ...

  10. 垃圾回收器及tomcat调优

    垃圾回收机制 1.概述:垃圾回收机制,Java中的对象不再有"作用域"的概念,只有对象的引用才有"作用域".垃圾回收可以有效的防止内存泄露,有效的使用空闲的内存 ...