image_2021-10-31_00-21-17.png
67.8 KB
#N1154. Day of the Year
problem link=>https://leetcode.com/problems/day-of-the-year/
#solution
problem link=>https://leetcode.com/problems/day-of-the-year/
#solution
class Solution {
public int dayOfYear(String date) {
int[] days={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year=Integer.parseInt(date.substring(0, 4));
int month=Integer.parseInt(date.substring(5, 7));
int isLeapYear=0;
int ans=0;
for(int i=0; i<month; i++){
ans+=days[i];
}
ans+=Integer.parseInt(date.substring(8, 10));
if(year%4!=0)
isLeapYear=0;
else if(year%100!=0)
isLeapYear=1;
else if(year%400!=0)
isLeapYear=0;
else
isLeapYear=1;
if(month<=2)
isLeapYear=0;
return ans+isLeapYear;
}
}