public class Solution {
    public int FindMinDifference(IList<string> timePoints) {
        var ary = new int[24 * 60];
            foreach (var time in timePoints)
            {
                var tp = time.Split(':');
                var hour = tp[0];
                var minute = tp[1];

                var h = Convert.ToInt32(hour[0].ToString()) * 10 + Convert.ToInt32(hour[1].ToString());
                var m = Convert.ToInt32(minute[0].ToString()) * 10 + Convert.ToInt32(minute[1].ToString());

                var index = h * 60 + m;
                if (ary[index] == 1)
                {
                    return 0;
                }
                else
                {
                    ary[index] = 1;
                }
            }
            var min = int.MaxValue;

            bool first = true;
            var firstNum = int.MaxValue;
            var lastNum = int.MinValue;
            var pre = 0;
            for (int i = 0; i < ary.Length; i++)
            {
                if (ary[i] == 1)
                {
                    if (first)
                    {
                        pre = i;
                        first = false;
                        firstNum = i;
                        continue;
                    }
                    var diff = Math.Abs(i - pre);
                    if (diff > 1440 / 2)
                    {
                        diff = 1440 - diff;
                    }
                    if (min > diff)
                    {
                        min = diff;
                    }
                    pre = i;
                    lastNum = i;
                }
            }

            min = Math.Min(min, (24 * 60 - lastNum + firstNum));

            return min;
    }
}

https://leetcode.com/problems/minimum-time-difference/#/description

posted on 2017-05-11 10:59  Sempron2800+  阅读(148)  评论(0编辑  收藏  举报