当前位置:网站首页>Sword finger offer II 035 Minimum time difference - quick sort plus data conversion

Sword finger offer II 035 Minimum time difference - quick sort plus data conversion

2022-07-07 01:20:00 Mr Gao

The finger of the sword Offer II 035. Minimum time difference - Quick sort plus data conversion

Given a 24 hourly ( Hours : minute “HH:MM”) Time list for , Find the minimum time difference between any two times in the list and express it in minutes .

Example 1:

Input :timePoints = [“23:59”,“00:00”]
Output :1

Example 2:

Input :timePoints = [“00:00”,“23:59”,“00:00”]
Output :0

The solution code is as follows :



void quick(int *a,int low,int high){
    
    if(low<high){
    
    // printf("**%d ",a[low]);
        int l=low,h=high,p=a[low];
        while(low<high){
    
            while(low<high&&a[high]>=p){
    
                high--;
            }
            a[low]=a[high];
            while(low<high&&a[low]<=p){
    
                low++;
            }
            a[high]=a[low];
        }
        a[low]=p;
        quick(a,l,low-1);
        quick(a,low+1,h);
    }
}

int findMinDifference(char ** timePoints, int timePointsSize){
    
    int  *a=(int *)malloc(sizeof(int )*timePointsSize);
    int i;
    for(i=0;i<timePointsSize;i++){
    

        char *s=timePoints[i];
     // printf("%s ",s);
        int z=0;
        int j=0;
        while(s[j]!=':'){
    
            z=z*10+s[j]-'0';
            j++;
        }
        int b=0;
        j++;
      
        while(s[j]!='\0'){
    
            b=b*10+s[j]-'0';
            j++;
        }
        a[i]=z*60+b;
    // printf("%d %d %d ",a[i],z,b);
         
    }
    quick(a,0,timePointsSize-1);

    int min=10000;
     int t=fmin(a[timePointsSize-1]-a[0],1440-a[timePointsSize-1]+a[0]);
    if(t<min){
    
            min=t;
        }
    for(i=1;i<timePointsSize;i++){
    
     // printf("-%d ",a[i]);
        int t=fmin(a[i]-a[i-1],1440-a[i]+a[i-1]);
        if(t<min){
    
            min=t;
        }
    }



return min;
}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061736354942.html