【leetcode❤python】 290. Word Pattern
#-*- coding: UTF-8 -*-
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
tag=0
tagdic={}
tagList=[]
i=0
while i<len(pattern):
if tagdic.has_key(pattern[i]):
tagList.append(tagdic.get(pattern[i]))
else:
tagdic.setdefault(pattern[i],tag)
tagList.append(tag)
tag+=1
i+=1
strList=str.split(' ')
tagdic={};tag=0;tagList2=[];i=0
while i<len(strList):
if tagdic.has_key(strList[i]):
tagList2.append(tagdic.get(strList[i]))
else:
tagdic.setdefault(strList[i],tag)
tagList2.append(tag)
tag+=1
i+=1
return tagList==tagList2
sol=Solution()
print sol.wordPattern('abba', 'dog cat cat dog')
【leetcode❤python】 290. Word Pattern的更多相关文章
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#290. Word Pattern
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- 【leetcode❤python】 165. Compare Version Numbers
#-*- coding: UTF-8 -*-class Solution(object): def compareVersion(self, version1, version2): ...
- 【leetcode❤python】 168. Excel Sheet Column Title
class Solution(object): def convertToTitle(self, n): """ :type n: in ...
随机推荐
- yii2细节设置
1.设置默认的跳转登陆页面.默认的登陆成功页 在项目的(backend/frontend的config中的main.php中的user组件中),添加loginUrl=>'admin/login' ...
- SSAS中角色(Role)定义需要注意的两个地方
开发过SSAS Cube的朋友应该都知道,我们可以在SSAS中设置若干个角色,把windows账号放入这些角色中来限制不同的windows账号可以看到的数据有哪些,这里有两点需要注意一下. 首先在Cu ...
- NOIP201405生活大爆炸版石头剪刀布
NOIP201405生活大爆炸版石头剪刀布 试题描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一 样,则不分胜负.在<生活大爆炸>第二季第 8 集中出现了 ...
- 关于UIWindow(转)
(原文出自:http://www.cnblogs.com/wendingding/p/3770052.html,特别感谢) 一:[[UIScreen mainScreen] bounds] 和[UIS ...
- yum源的修改
源路径: /etc/yum.repos.d/ 配置文件: 网络搜索 CentOS-Base.repo(默认) 设备搜索 CentOS-Media.repo 将CentOS-Base.repo移除或改名 ...
- Xcode 项目配置学习
Xcode有四种build for 分别是: build for Running build for Testing build for Profiling build for Archiving R ...
- Add baidu map in your website (wordpress)
手动挡 访问应用(AK)Key http://lbsyun.baidu.com/apiconsole/key Basic Map Generator http://api.map.baidu.com/ ...
- href="#"会导致location.replace(location.href);脚本不工作
我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...
- VPS常用工具
1.命令行工具 putty 在Mac下,可以直接使用超级终端 ssh username@ipaddress 2.可视化上传文件工具 WinSCP 在Mac下,使用 Cyberduck
- bootstrap 列表 表格 表单 复选 单选 多选 输入框组
一.列表 ul li 二.表格 table (http://www.runoob.com/bootstrap/bootstrap-tables.html) 1. 基本表格 <table cla ...