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