leetcode Day42 剑指专项34

84 阅读1分钟

剑指 Offer II 034. 外星语言是否排序

var isAlienSorted = function(words, order) {
    let map=new Map()
    for(let i=0;i<order.length;i++){
        map.set(order[i],i)
    }
    map.set(undefined,-1)
    for(let i=0;i<words.length-1;i++){
        for(let j=0;j<Math.max(words[i].length,words[i+1].length);j++){
            if(words[i][j]===words[i+1][j]){
                continue
            }else if(map.get(words[i][j])>map.get(words[i+1][j])){
                return false
            }else{
                break
            }
        }
    }
    return true
};

剑指 Offer II 035. 最小时间差

const timePoints = ["23:59","00:00","02:56","23:14"]
var findMinDifference = function(timePoints) {
    timePoints.sort()
    let res=Math.min((getMinite(timePoints[timePoints.length-1])-getMinite(timePoints[0])),(getMinite(timePoints[0])+1440-getMinite(timePoints[timePoints.length-1])))
    for(let i=0;i<timePoints.length-1;i++){
        res=Math.min(res,getMinite(timePoints[i+1])-getMinite(timePoints[i]))
    }
    return res
};
const getMinite=(s)=>{
    let times=s.split(':').map(i=>i*1)
    return times[0]*60+times[1]
}