笔试请教
大佬求教,信也科技笔试第一题感觉是一个简单的求日期是一年的第几天,但就是只过了20%,有没有大佬帮忙看看哪里出问题了qvq
public class XingYe0922 {
public int dateArraysSum(String[] dates) {
// write code here
int ans = 0;
if (dates.length == 0 || dates == null) {
return 0;
}
// Arrays.sort(dates, new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
//
// return Integer.parseInt(o1.split(" ")[2])-Integer.parseInt(o2.split(" ")[2]);
// }
// });
for (int i = 0; i < dates.length; i++) {
if (i > 0 && dates[i].equals(dates[i - 1])) continue;
ans += helper(dates[i]);
}
return ans;
}
public int helper(String s) {
int ans = 0;
int m1 = 30, m2 = 31, feb = 0;
String[] str = s.split(" ");
String year = str[2];
String month = str[1];
String day = str[0];
if (Integer.parseInt(year) % 400 == 0 || Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) {
feb = 29;
} else {
feb = 28;
}
int m = 0;
if (month.equals("Jan")) m = 1;
else if (month.equals("Feb")) m = 2;
else if (month.equals("Mar")) m = 3;
else if (month.equals("Apr")) m = 4;
else if (month.equals("May")) m = 5;
else if (month.equals("Jun")) m = 6;
else if (month.equals("Jul")) m = 7;
else if (month.equals("Aug")) m = 8;
else if (month.equals("Sep")) m = 9;
else if (month.equals("Oct")) m = 10;
else if (month.equals("Nov")) m = 11;
else if (month.equals("Dec")) m = 12;
int d = 0;
int index = 0;
String day2 = "";
while (s.charAt(index) <= '9' && s.charAt(index) >= '0' && index < day.length()) {
day2 += s.charAt(index);
index++;
}
d = Integer.parseInt(day2);
switch (m - 1) {
case 11:
ans += m2;
case 10:
ans += m1;
case 9:
ans += m2;
case 8:
ans += m1;
case 7:
ans += m1;
case 6:
ans += m2;
case 5:
ans += m1;
case 4:
ans += m2;
case 3:
ans += m1;
case 2:
ans += feb;
case 1:
ans += m1;
case 0:
ans += d;
break;
}
return ans;
}
}
#笔试#
360集团公司福利 405人发布
查看8道真题和解析