
1154. Day of the YearGiven a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, returnthe day number of the year.Example 1:Input:date “2019-01-09”Output:9Explanation:Given date is the 9th day of the year in 2019.Example 2:Input:date “2019-02-10”Output:41Constraints:date.length 10date[4] date[7] ‘-’, and all other date[i]s are digitsdate represents a calendar date betweenJ a n 1 s t , 1900 Jan 1^{st}, 1900Jan1st,1900andD e c 31 s t , 2019 Dec 31^{st}, 2019Dec31st,2019.From: LeetCodeLink: 1154. Day of the YearSolution:Ideas:Parse year, month, day, sum the days of all previous months, add the current day, and add 1 extra day if it’s a leap year and the month is after February.Code:intdayOfYear(char*date){intyear,month,day;sscanf(date,%d-%d-%d,year,month,day);intdays[12]{31,28,31,30,31,30,31,31,30,31,30,31};intansday;for(inti0;imonth-1;i){ansdays[i];}intleap(year%4000)||(year%40year%100!0);if(leapmonth2){ans;}returnans;}