LeetCode 953 Verifying an Alien Dictionary 解题报告
题目要求
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.
题目分析及思路
给定一组字符串以及一个字母表顺序,判断这组字符串是否是按照给定字母表顺序排列的。可以对该组字符串进行两两比较,依据最短字符串长度分别遍历两个字符串。若前者的字符顺序小于后者的,则跳出循环,继续比较其他字符串;若前者的字符顺序大于后者的,则返回False;若相等则继续比较,直到所有字符都相等,若较长字符串在前则返回False。
python代码
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
for idx in range(1, len(words)):
len_min = min(len(words[idx-1]), len(words[idx]))
flag = 0
while len_min:
if order.index(words[idx-1][flag]) < order.index(words[idx][flag]):
break
elif order.index(words[idx-1][flag]) > order.index(words[idx][flag]):
return False
len_min -= 1
flag += 1
if len_min == 0 and len(words[idx-1]) > len(words[idx]):
return False
return True
LeetCode 953 Verifying an Alien Dictionary 解题报告的更多相关文章
- 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 953. Verifying an Alien Dictionary
原题链接在这里:https://leetcode.com/problems/verifying-an-alien-dictionary/ 题目: In an alien language, surpr ...
- LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)
题目标签:HashMap 题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序. 利用hashmap 把order 存入 map, ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- 【leetcode】953. Verifying an Alien Dictionary
题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...
- 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...
- 953.Verifying an Alien Dictionary(Map)
In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...
- Verifying an Alien Dictionary
2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
随机推荐
- 业界常用的和不常用cad快捷键
AutoCAD 是目前世界各国工程设计人员的首选设计软件,简便易学.精确无误是AutoCAD成功的两个重要原因.AutoCAD提供的命令有很多,绘图时最常用的命令只有其中的百分之二十. 在CAD软件操 ...
- centos7目录统计之du命令
CentOS下du查看计算目录大小的命令 用法实例: [root@localhost local]# du -hs smgpdfd 3.3G smgpdfd [root@localhost lo ...
- Mac: Alias[设置命令的别名]
设置`.bash_profile` 1.打开终端`Terminal` 2.输入命令`cd ~`到用户主目录 $ cd ~ 3.生成一个新文件 $ touch .bash_profile 4.使用喜欢的 ...
- Markdown 代码
我们使用三个反引号来标记代码,语法及效果如下: ```python #!/usr/bin/env python #-*- coding:utf- -*- print("Hello World ...
- 用addOnGlobalLayoutListener获取View的宽高
首先,我们在onCreate方法里调用getHeight()和 getWidth()是不能正确获取View的宽高的,因为onCreate方法执行完了,我们定义的控件才会被onMeasure()度量,所 ...
- [IOI 2000]POJ 1160 Post Office
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...
- manjaro 设置 国内源
注意,如果安装过程中出现无法连接服务,请参看第 4条. 1. 设置官方镜像源(包括 core, extra, community, multilib ) $ sudo pacman-mirrors - ...
- CRUD的操作,增删改查!
.注释语法:--,# .后缀是.sql的文件是数据库查询文件 .在创建查询里,那个需要保存的对话框只是,保存查询. .在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作: create 创 ...
- git命令无法自动补全(sles11.3)
找到git-completion bash文件 find / -name 'git-completion' /usr/share/doc/git-1.7.1/contrib/completion/gi ...
- [No0000174]Spring常用注解(收藏大全)
Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...