share my easy understandable java solution!


  • 0
    T
    public class Solution {
        public int findMinDifference(List<String> timePoints) {
            if(timePoints==null || timePoints.size()<=1) return 0;
            
            Collections.sort(timePoints,new Comparator<String>(){
                  public int compare(String a,String b){
                      return a.compareTo(b);
                  }
            });
            
            int min=Integer.MAX_VALUE;
            for(int i=1;i<timePoints.size();i++){
                String[] str1=timePoints.get(i-1).split(":");
                String[] str2=timePoints.get(i).split(":");
                
                int h1=Integer.parseInt(str1[0]);
                int m1=Integer.parseInt(str1[1]);
                
                int h2=Integer.parseInt(str2[0]);
                int m2=Integer.parseInt(str2[1]);
                
                int dif1=h2*60-h1*60+m2-m1;
                min=Math.min(min,dif1);
            }
            
            String[] str1=timePoints.get(timePoints.size()-1).split(":");
            String[] str2=timePoints.get(0).split(":");
                
            int h1=Integer.parseInt(str1[0]);
            int m1=Integer.parseInt(str1[1]);
                
            int h2=Integer.parseInt(str2[0]);
            int m2=Integer.parseInt(str2[1]);
            
            int dif2=24*60-(h1*60+m1)+h2*60+m2;
            min=Math.min(dif2,min);
            
            return min;
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.