Word Pattern解题报告
2016-09-18 21:37
253 查看
https://leetcode.com/problems/word-pattern/
题目描述见链接
这道题开始我觉得很简单,把string分割之后,然后把分割之后的字符串和位置进行map。但是后来发现如果有重复的就不好处理,后来发现可以用两个hashmap,将string分割之后的字符和pattern中的字符进行映射,然后如果不相等,则报错。这样比较好的一点是可以支持多种pattern比如abcdaaabcd
代码:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
sl=str.split()
if len(sl)!=len(pattern):
return False;
p={}
sd={}
for ptn,s in zip(pattern,sl):
if ptn not in p:
p[ptn]=s
if s not in sd:
sd[s]=ptn
if p[ptn]!=s or sd[s]!=ptn:
return False
return True
在网上还看到了一个非常简洁的python:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
s = pattern
t = str.split()
return map(s.find, s) == map(t.index, t)
map函数的官方描述是:
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another
it is assumed to be extended withNoneitems. If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation).
The iterable arguments may be a sequence or any iterable object; the result is always a list.
也就是对list中的每一个元素实施指定的方法,返回的结果作为一个数组。
这个方法真是太厉害了。
题目描述见链接
这道题开始我觉得很简单,把string分割之后,然后把分割之后的字符串和位置进行map。但是后来发现如果有重复的就不好处理,后来发现可以用两个hashmap,将string分割之后的字符和pattern中的字符进行映射,然后如果不相等,则报错。这样比较好的一点是可以支持多种pattern比如abcdaaabcd
代码:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
sl=str.split()
if len(sl)!=len(pattern):
return False;
p={}
sd={}
for ptn,s in zip(pattern,sl):
if ptn not in p:
p[ptn]=s
if s not in sd:
sd[s]=ptn
if p[ptn]!=s or sd[s]!=ptn:
return False
return True
在网上还看到了一个非常简洁的python:
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
s = pattern
t = str.split()
return map(s.find, s) == map(t.index, t)
map函数的官方描述是:
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another
it is assumed to be extended withNoneitems. If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation).
The iterable arguments may be a sequence or any iterable object; the result is always a list.
也就是对list中的每一个元素实施指定的方法,返回的结果作为一个数组。
这个方法真是太厉害了。
相关文章推荐
- Codeforces Round #372 (Div. 2) C. Plus and Square Root【二分】
- 内核之框架介绍及简单修改
- codeforces 712A. Memory and Crow
- 一个 -100.01 的double 在内存中怎么存储的. 一个中文String 在内存中占多少直接 utf-8 / GBK
- 淡入版轮播图
- Android总结篇系列:Android广播机制
- JSON解析
- 前偶后奇奇偶分离
- 浪潮编程题
- 菜鸟的日常安卓简单之路(1)
- 根据类名来跳转对应界面
- eclipse birt报表开发入门
- 141. Linked List Cycle(python)
- shape文件属性
- 【JavaScript】 Webpack安装及文件打包
- 二次、三次贝塞尔曲线demo(演示+获取坐标点)
- hdu 5894 hannnnah_j’s Biological Test 排列组合
- MySQL锁机制与MVCC原理--推荐阅读
- Java 迷宫问题,复杂多条件迷宫问题
- Ipython