Day of the Year LeetCode solution in Java

1
class Solution {
2
public int dayOfYear(String date) {
3
int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
4
int year =
5
(date.charAt(0) - 48) * 1000
6
+ (date.charAt(1) - 48) * 100
7
+ (date.charAt(2) - 48) * 10
8
+ date.charAt(3)
9
- 48;
10
if (year % 4 == 0 && year != 1900) {
11
months[1]++;
12
}
13
int ans = 0;
14
for (int i = 0; i < ((date.charAt(5) - 48) * 10) + date.charAt(6) - 48 - 1; i++) {
15
ans += months[i];
16
}
17
ans += (date.charAt(8) - 48) * 10 + date.charAt(9) - 48;
18
return ans;
19
}
20
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0