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 ...
随机推荐
- java 8 stream使用
使用stream代替循环的方案 1.定义一个Article类包括标题.作者.标签 private class Article { private final String title; private ...
- Microsoft SQL Server 存储过程
Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...
- document.write() 和 document.writeln区别
document.write() 和 document.writeln 都是JavaScript向客户端写入的方法,writeln是以行方式输出的,但并不是指页面实际效果中的换行,两种方法在查看源代码 ...
- enote笔记语言(2)(ver0.4)
why not(whyn't) 为什么不(与“why”相反对应,是它的反面) how对策 how设计 key-memo: ...
- Re0:DP学习之路 饭卡 HDU - 2546
解法 01背包变式,首先贪心的想一下如果要保证余额最小那么就需要用相减后最小的钱减去之前最大的价格,且得保证这个钱在5元以上 对于寻找如何减最多能包含在5元以上,这里用01背包 我们把价钱看做体积装进 ...
- IntelliJ IDEA 环境设置——侧栏显示类中所有方法
myeclipse默认会在右侧栏显示类的所有方法框,但是IDEA里并没有这样的初始化设置 那么怎样显示这个功能? 1.点击工具栏View-->Tool Windows-->Structur ...
- 第十一节:Web爬虫之数据存储(数据更新、删除、查询)
接着上一节的内容 5.MySQL数据更新 结果是将id=3的name进行更新操作,结果如下: 6.MySQL数据去重及更新 结果是判断数据是否有重复的,如果有重复的将不再存储,若没有重复的就更新数据进 ...
- pressure coeffcient of a wing/blade
software: CFD POST ANSYS menu bar, select Tools > Macro Calculator. \ correction: Ref pressure is ...
- Linux 复习二
第二章 一.Linux文件系统 1.基本概念 Linux文件系统为单根的树状结构,根为“/”,文件名大小写敏感,除了“/”都是可用字符,文件名以“.”开始的为隐藏文件. 2.常用文件夹 bin:可执行 ...
- excel 2003 默认保存后出现超级连接解决方法
在excel 2003 中当选中某个单元格然后拷贝出来后发现总是出现超级连接,每次都要取消下很是麻烦 . 于是经过研究找到解决方法,真是累的我够呛 ,先将方法介绍给大家. 工具---自动更正选项--- ...