1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { int[] days=new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) { int[] alice=new int[2]; int[] bob=new int[2]; alice[0]=dateToIndex(arriveAlice); alice[1]=dateToIndex(leaveAlice); bob[0]=dateToIndex(arriveBob); bob[1]=dateToIndex(leaveBob);
if((alice[0]<bob[0]&&alice[1]<bob[0])||(alice[0]>bob[1]&&alice[1]>bob[1])){ return 0; }else{ return Math.min(alice[1],bob[1])-Math.max(alice[0],bob[0])+1; } }
int dateToIndex(String dateStr){ int[] date=new int[2]; date[0]=(dateStr.charAt(0)-'0')*10+(dateStr.charAt(1)-'0'); date[1]=(dateStr.charAt(3)-'0')*10+(dateStr.charAt(4)-'0'); int ret=0; for(int i=0;i<date[0]-1;i++){ret+=days[i];} ret+=date[1]; return ret; } }
|