zoukankan      html  css  js  c++  java
  • 953.Verifying an Alien Dictionary(Map)

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
    Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words 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.

    使用Map将字典序映射为数字,进行比较

    class Solution {
        public boolean compare(Map<Character,Integer> ch,String string1,String string2){
            int len1 = string1.length();
            int len2 = string2.length();
            int len = len1 < len2 ?len1:len2;
            for(int i =0;i<len;i++){
                if(ch.get(string1.charAt(i)) < ch.get(string2.charAt(i)))  return true;
                else if(ch.get(string1.charAt(i)) == ch.get(string2.charAt(i)))
                    continue;
                else
                    return false;
            }
            if(len1<=len2)   return true;
            else    return false;
        }
        public boolean isAlienSorted(String[] words, String order) {
            Map<Character,Integer> ch = new HashMap<Character,Integer>();
            for(int i =0;i<order.length();i++){
                ch.put(order.charAt(i),i);
            }
            for(int i=1;i<words.length;i++){
                if(!compare(ch,words[i-1],words[i])) return false;
            }
            return true;
        }
    }
    
  • 相关阅读:
    0.0pomelo的优缺点
    python操作MySQL
    MySQL-基本查询语句及方法,连表和子查询
    MySQL-外键对应关系
    MySQL--存储引擎、数据类型、约束条件
    数据库MySQL安装、基本指令
    并发编程-协程、池,io模型
    python并发编程-GIL全局解释锁,Event事件,信号量
    并发编程-线程
    并发编程-进程
  • 原文地址:https://www.cnblogs.com/a1225234/p/10284599.html
Copyright © 2011-2022 走看看