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 ...
随机推荐
- git删除本地分支失败,报错error: branch 'test219' not found.
错误: 删除本地分支报错,操作如下: git branch -d test219 操作失败,错误信息:error: branch 'test219' not found git branch -D t ...
- C++(变量类型-深入)
变量类型 变量其实只不过是程序可操作的存储区的名称.C++ 中每个变量都有指定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上. 变量的名称可以由字母.数字 ...
- MSSQLSERVER_3176
本文档已存档,并且将不进行维护. MSSQLSERVER_3176 SQL Server 2014 SQL Server 2012 ...
- TI 77GHZ雷达开发套件 RDP-DC100
RDP-DC100用户使用手册 目录 1. 硬件说明... 3 1.1. 官方处理板的修 ...
- sublime text3 =个人插件
1.sublime text3汉化插件安装. ctrl+shift+p → Package Control:Install Package → ChineseLocalization preferen ...
- 安装FCIS问题汇总
安装官网安装步骤时可能出现的问题: "/usr/bin/ld: cannot find -lopenblas" error 解决方案: apt install liblapack- ...
- HDU_1875_畅通工程再续
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- arx升级
如果你打算升级你的ARX或者想在同一个IDE(譬如vs2010)编译多个版本的ARX,那么我希望这篇帖子对你有帮助首先你应该简单了解Objectarx开发的版本对应情况:R15 --- 2000- ...
- POJ2454——Jersey Politics
POJ2454——Jersey Politics 题目大意: 在泽西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷仓中获得了三个档位. 泽西奶牛目前控制着国家重新分配委员会. 他们想将国家分为三个相当 ...
- bootstrap-table使用笔记
服务端分页: //html <div class="container-fluid"> <div style="margin-top:1em" ...