【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
【leetcode】816. Ambiguous Coordinates的更多相关文章
- 【LeetCode】816. Ambiguous Coordinates 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/ambiguous ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- 如何把一些字符串用dict组织成json格式?(小算法)
说明: 1. 数据库中的一条记录取出来是这样的(直接复制):'value1','value2' ,'value3' 2. 我希望使用的数据格式是:{key1:'value1',key2:'value2 ...
- 通过git新增、更新代码内容到github
github可用于个人用户托管公开项目,对于异地上传下载十分方便 1. 准备工作 2. 首次上传执行命令集合 3. 更新执行命令集合 4. 命令总结 1.准备工作 a.注册github帐号 , ...
- 黑群晖DSM 6.x 配置文件grub.cfg修改 mac地址/sn等修改
新的DSM 6.x配置文件和以前的XPEnoboot的配置文件不一样了,我们可以通过OSFMount虚拟光驱软件打开img后再修改. 安装完成后运行OSFMount点击左下角-Mount new,选择 ...
- Maven系列学习(三)Maven生命周期和插件
Maven生命周期和插件 Maven另外的两个核心概念就是生命周期和插件,Maven的生命周期都是抽象的,其实实际行为都是由插件来完成的,生命周期和插件两者协同工作 1.生命周期 Maven的生命周期 ...
- Python3的基本数据类型
2.1. Python3中六个标准的基本数据类型: Number(数字) String(字符串) Sets(集合) Tuple(元组) List(列表) Dictionary(字典) 2.2. Pyt ...
- Redis--小小总结
1.基本定义 memcached是纯粹的key-value内存数据库,也可能不应该叫数据库,应该叫另类缓存技术: Redis是一个基于内存的高性能key-value数据库:将数据全部加载到内存中,并定 ...
- 扩展欧几里得算法详解(exgcd)
一.前言 本博客适合已经学会欧几里得算法的人食用~~~ 二.扩展欧几里得算法 为了更好的理解扩展欧几里得算法,首先你要知道一个叫做贝祖定理的玄学定理: 即如果a.b是整数,那么一定存在整数x.y使得$ ...
- H5白屏问题
前言 前阵子弄了灰度环境,H5这边需要给灰度环境的接口加上Cookie,配置的期间遇到一些Cookie问题以及白屏在此记录下 1.H5请求接口带不上Cookie 解决方法:前端使用了 webpack ...
- R语言基础篇——数据读写
1.键盘输入数据(适合小数据集) #创建一个指定模式但不含数据的变量 mydata<-data.frame(age=numeric(0),gender=character(0),weight=n ...
- VS2015-MFC基础教程-应用程序工程中文件的组成结构
VS2015应用程序向导生成框架程序后,我们可以在之前设置的Location下看到此文件夹中包含了几个文件和一个以工程名命名的子文件夹,这个子文件夹中又包含了若干个文件和一个res文件夹,创建工程时的 ...