0
点赞
收藏
分享

微信扫一扫

LeetCode刷题篇之第539道算法题的解题思路


leetcode刷题系列:

​​原创 LeetCode刷题篇之第1道算法题的解题思路​​

​​原创LeetCode刷题篇之第43道算法题的解题思路​​

​​原创 LeetCode刷题篇之第58道算法题的解题思路​​

​​原创 LeetCode刷题篇之第67道算法题的解题思路​​

​​原创LeetCode刷题篇之第164道算法题的解题思路​​

​​原创 LeetCode刷题篇之第189道算法题的解题思路​​

​​原创 LeetCode刷题篇之第268道算法题的解题思路​​

​​原创 LeetCode刷题篇之第557道算法题的解题思路​​

题目:

LeetCode刷题篇之第539道算法题的解题思路_javascript

解题思路:

<!--//*<!DOCTYPE html>*//-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>leetcode刷题第536题的解题思路</title>
</head>
<script>
let findMinDifference = function (timePoints) {
timePoints = timePoints.sort();
console.log(timePoints)
for (let i = 0; i < timePoints.length; i++) {
let Array1 = timePoints[i].split(":");
timePoints[i] = parseInt(Array1[0])*60 + parseInt(Array1[1]);

}
//console.log("timePoints)


let min = 1440 - timePoints[timePoints.length-1] + timePoints[0];
for (let i = 0; i < timePoints.length - 1; i++) {
if((timePoints[i+1]-timePoints[i])>=720){//大于半圆,最短算另外一边
if (min>=(1440-timePoints[i+1]+timePoints[i])){
min =1440-timePoints[i+1]+timePoints[i];
}
}else{//差值在半圆以内,直接减
if(min>=timePoints[i+1]-timePoints[i]){
min = timePoints[i+1]-timePoints[i];
}
}
}

console.log(min)
}
findMinDifference(["10:00","23:59","00:00"]);//测试用例
</script>
<body>

</body>

</html>

示意图

LeetCode刷题篇之第539道算法题的解题思路_javascript_02

答案:

var findMinDifference = function(timePoints) {
timePoints = timePoints.sort();
for (let i = 0; i < timePoints.length; i++) {
let Array1 = timePoints[i].split(":");
timePoints[i] = parseInt(Array1[0])*60 + parseInt(Array1[1]);
}
let min = 1440 - timePoints[timePoints.length-1] + timePoints[0];
for (let i = 0; i < timePoints.length - 1; i++) {
if((timePoints[i+1]-timePoints[i])>=720){
if (min>=(1440-timePoints[i+1]+timePoints[i])){
min =1440-timePoints[i+1]+timePoints[i];
}
}else{
if(min>=timePoints[i+1]-timePoints[i]){
min = timePoints[i+1]-timePoints[i];
}
}
}
return min;
};


LeetCode刷题篇之第539道算法题的解题思路


当然,这题不止一种解题方法,还有好多。如果你有思路欢迎评论区留言。

如果你正好在做这道题,看到我的文章有了思路,那就点赞留言关注一下撒!!!

欢迎三连!!!????????????​

Github:​​zhongzhimao​​

杰灵软件:​​杰灵软件官网​​

微信公众号:杰灵软件

LeetCode刷题篇之第539道算法题的解题思路_javascript_03



举报

相关推荐

0 条评论