[LeetCode Daily Question] 1904. The number of complete games you have completed

Give you two stringsstartTime and finishTime, both of which conform to the format of "HH:MM", representing you respectively completed. complete games the exact time you spent in the game, please calculate Number of Exit andEnter

IffinishTime is earlier than startTime, it means you played all night (that is, from startTime to midnight, and from midnight to finishTime).

• For example, if startTime = "05:20" and finishTime = "05:59" , it means that you only complete from 05:30 to 05:45 This is a complete game. And you have not completed the complete game from 05:15 to 05:30 because you entered the game after the game started; at the same time, you have not completed the game from A complete game from a> because you exited the game before the end of the game. 05:45 to 06:00

Assuming you entered the game from startTime and exited the game at finishTime, please calculate and return your completed Number of complete games.

The idea is as follows:

Insert image description here

Code:

var numberOfRounds = function(loginTime, logoutTime) {
    
    
    let loginTimeArr = loginTime.split(':');
    // let login = loginTimeArr[0]*60+Math.ceil(loginTimeArr[1]/15)*15;

    // 字符串加数字会拼接起来
    // let login = loginTimeArr[0]*60+loginTimeArr[1]

    let login = loginTimeArr[0]*60+Number(loginTimeArr[1])
    let logoutTimeArr = logoutTime.split(':');
    let logout = logoutTimeArr[0]*60+Number(logoutTimeArr[1])

    if(logout<login){
    
    
        logout+=24*60;
    }
    login = login-loginTimeArr[1] + Math.ceil(loginTimeArr[1]/15)*15;
    logout = logout-logoutTimeArr[1] + Math.floor(logoutTimeArr[1]/15)*15;
    return (logout-login)/15>0?(logout-login)/15:0;

    // 遗漏掉了一种情况 就是 一个向上取整,一个向下取整,然后logout< login
    // "00:47" "00:57"  应该先比较后对时间进行处理

};

Guess you like

Origin blog.csdn.net/qq_43720551/article/details/134985265