LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)
题目标签:HashMap
题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序。
利用hashmap 把order 存入 map, 写一个helper method 来判断每临近的两个 word 是否排序正确。
遍历words array,依次比较临近的两个words。
具体看code。
Java Solution:
Runtime: 5 ms, faster than 64.55%
Memory Usage: 38.2 MB, less than 11.41%
完成日期:03/14/2019
关键点:把order 存入map
class Solution {
public boolean isAlienSorted(String[] words, String order) {
if(words.length == 1)
return true;
Map<Character, Integer> map = new HashMap<>();
// put order into hashmap: key = char, value = index
for(int i=0; i<order.length(); i++)
{
map.put(order.charAt(i), i);
}
// iterate words to compare each two
for(int i=1; i<words.length; i++)
{
if(!isSorted(words[i-1], words[i], map))
return false;
}
return true;
}
private boolean isSorted(String word1, String word2, Map map)
{
int size = Math.max(word1.length(), word2.length());
for(int i=0; i<size; i++)
{
if(i >= word1.length())
return true;
else if(i >= word2.length())
return false;
int index1 = (int)map.get(word1.charAt(i));
int index2 = (int)map.get(word2.charAt(i));
if(index2 < index1)
return false;
else if(index1 < index2)
return true;
}
return true;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)的更多相关文章
- Leetcode953. Verifying an Alien Dictionary验证外星语词典
某种外星语也使用英文小写字母,但可能顺序 order 不同.字母表的顺序(order)是一些小写字母的排列. 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在 ...
- 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_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary
In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...
- 【leetcode】953. Verifying an Alien Dictionary
题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...
- 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
随机推荐
- JPQL 模糊查询,查询条件拼接(like使用)
@Transactional public List<ViewCorplist2> findAllCorpsLikeK(String kw) { System.out.println(kw ...
- PHP fSQL Tutorial
PHP fSQL Tutorial This tutorial is designed to give a brief overview of the PHP fSQL API since versi ...
- jsp学习笔记 - 内置对象 config
1.将页面保存在 WEB-INF文件夹下是最安全的,一般不可见 可以通过映射路径来进行访问 2.通过config对象可以取得初始化的配置参数 String dbDriver = config.getI ...
- RabbitMQ系列(三)--Java API
基于java使用RabbitMQ 框架:SpringBoot1.5.14.RELEASE maven依赖: <dependency> <groupId>com.rabbitmq ...
- ThinkPHP---案例2--职员管理功能
[一]准备工作 (1)创建菜单,修改跳转路径 <li> <a href="javascript:;" class="workerManage" ...
- 第二节:1_C#中的委托的使用和讲解(转)
C# 中的委托 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容 ...
- <MySQL>入门六 变量
/* 变量 系统变量: 全局变量 会话变量 自定义变量 用户变量 局部变量 */ -- ------------系统变量-------------------- /* 变量由系统提供,不是用户定义,属 ...
- fiddler培训
fiddler 在客户端和服务器中间做一个代理 ,只能截获http或HTTPS的请求 代理地址127.0.0.1 端口8888 反向代理,正向代理 浏览器上设置代理地址和端口 左边是session ...
- PAT 1133 Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- CodeForcesGym 100753A A Journey to Greece
A Journey to Greece Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeFor ...