题目标签: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 (验证外星语词典)的更多相关文章

  1. Leetcode953. Verifying an Alien Dictionary验证外星语词典

    某种外星语也使用英文小写字母,但可能顺序 order 不同.字母表的顺序(order)是一些小写字母的排列. 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在 ...

  2. LeetCode 953. Verifying an Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/verifying-an-alien-dictionary/ 题目: In an alien language, surpr ...

  3. LeetCode 953 Verifying an Alien Dictionary 解题报告

    题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...

  4. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  5. [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  6. 【leetcode】953. Verifying an Alien Dictionary

    题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...

  7. 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 953.Verifying an Alien Dictionary(Map)

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  9. Verifying an Alien Dictionary

    2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...

随机推荐

  1. Burp Suite抓https数据包

    本地环境JDK1.8Burp Suite 1.7.26 Firefox 59.0.2 一.burp介绍请自行谷歌,这里不过多介绍 二.配置HTTPS抓包方法[以Firefox为例]通常情况下burp默 ...

  2. python __slots__ 详解(上篇)

    转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...

  3. CommHelper

    18位流水号: public static string GenerateTransId(int i) { string transId = DateTime.Now.ToString("y ...

  4. 【笔记JS/HTML/CSS】ubuntu环境下的sublime text2 安装 zenCoding

    刚接触web编程的时候就被老师安利了sublime text2 这个文本编辑器,后来发现它真的挺好用的,无论是windows还是ubuntu,都可以很简单地下载安装(到官网,免费哦),三分钟内就搞定了 ...

  5. centos右上角wired图标消失有效解决方案

    最近在学习Linux配置nginx时,左上角的wired图标突然没了,很神奇.然后在网上按着很多博客说的去改,都没用,最后终于根据下面参考博客内的方案解决了问题,嘿嘿. mv /var/lib/Net ...

  6. ANNOTATION and analyse hello1.java

    一.What is annotation? annotation的中文意思就是注解,注释的意思.注解也属于一种类型.它是在 Java SE 5.0 版本中开始引入的概念.它的形式跟接口很类似,不过前面 ...

  7. input password密码验证跳转页面

    代码如下: 查询密码 <input type="password" id="pwd" /> 页面如下: 密码校验成功后跳转页面: window.lo ...

  8. 网络编程 - 简单的socket例子

    1.客户端 #客户端import socketclient=socket.socket() #生成socket连接对象client.connect(("localhost",696 ...

  9. 12Cookie、Session

    12Cookie.Session-2018/07/24 1.保存会话数据 cookie客户端技术,把每个用户的数据以cookie的形式写给用户各自的浏览器 HttpSession服务端技术,服务器运行 ...

  10. 【ssm】spring功能讲解

    概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...