【leetcode】953. Verifying an Alien Dictionary
题目如下:
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different
order
. Theorder
of the alphabet is some permutation of lowercase letters.Given a sequence of
words
written in the alien language, and theorder
of the alphabet, returntrue
if and only if the givenwords
are sorted lexicographicaly in this alien language.Example 1:
- Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
- Output: true
- Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
- Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
- Output: false
- Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
- Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
- Output: false
- Explanation: The first three characters "app" match, and the second string is shorter (in size.)
According to lexicographical rules "apple" > "app", because 'l' > '∅',
where '∅' is defined as the blank character which is less than any other character (More info).Note:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
- All characters in
words[i]
andorder
are english lowercase letters.
解题思路:题目很长,但是很简单,说的简单点就是自定义了一个字典序,用这个自定义的字典序判断数组中字符串是否是升序的。
代码如下:
- class Solution(object):
- def isAlienSorted(self, words, order):
- """
- :type words: List[str]
- :type order: str
- :rtype: bool
- """
- dic = {}
- for i,v in enumerate(order):
- dic[v] = i
- def cmpf(s1,s2,dic):
- for i in range(min(len(s1),len(s2))):
- if dic[s1[i]] > dic[s2[i]]:
- return 1
- elif dic[s1[i]] < dic[s2[i]]:
- return -1
- if len(s1) > len(s2):
- return 1
- else:
- return -1
- for i in range(len(words)-1):
- if cmpf(words[i],words[i+1],dic) == 1:
- 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_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
原题链接在这里:https://leetcode.com/problems/verifying-an-alien-dictionary/ 题目: In an alien language, surpr ...
- LeetCode 953 Verifying an Alien Dictionary 解题报告
题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...
- LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)
题目标签:HashMap 题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序. 利用hashmap 把order 存入 map, ...
- 953.Verifying an Alien Dictionary(Map)
In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- Verifying an Alien Dictionary
2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- jQuery HTML- 添加元素
添加内容 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- JavaSE---多线程---概述
1.概述 1.1 进程: 系统进行资源分配.调度的一个独立单元: 进程的特征: 1.1.1 独立性: 系统中独立存在的实体,拥有自己独立的资源: 每个进程都拥有自己私有的地址空间,在没有经过进程本身允 ...
- 记录 SpringBoot 踩坑经历
1.spring-boot-starter-web 作用 <dependency> <groupId>org.springframework.boot</groupId& ...
- 模板方法模式TemplateMethod
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11407071.html 1. 定义定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子 ...
- boost smart pointer
1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and ...
- spring的 onApplicationEvent方法被执行两次问题
原文地址:http://www.cnblogs.com/a757956132/p/5039438.html 在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. ...
- 架构师技能树skill-map
# 架构师技能树 ## 系统架构能力 ### 基本理论- 扩展性设计- 可用性设计- 可靠性设计- 一致性设计- 负载均衡设计- 过载保护设计 ### 协议设计- 二进制协议- 文本协议 ### 接入 ...
- 88、展示Tensorflow计算图上每个节点的基本信息以及运行时消耗的时间和空间
''' Created on May 24, 2017 @author: p0079482 ''' #使用程序输出日志 import tensorflow as tf with tf.Session( ...
- Linux / Unix Command: rz
yum install lrzsz Most communications programs invoke rz and sz automatically. You can also connect ...
- RTTI RAII
RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的 ...