我參加了一個挑戰,其中要求是比較,如果t2
之前發生t1
兩個時間值t1
和t2
和print First
;否則,print Second
。比較兩個時間值
And,t1 != t2
。
樣品輸入:
2
10:19PM 02:49AM
08:49AM 09:10AM
樣本輸出:
Second
First
我的代碼:
import java.util.*;
public class Solution {
static String timeCompare(String t1, String t2){
// Complete this function
String half1 = t1.substring(t1.length()-2); // gets AM/PM value
String half2 = t2.substring(t2.length()-2);
String time1 = t1.substring(0, t1.length()-2);
String time2 = t2.substring(0, t2.length()-2);
//System.out.println(time1);
int hour1 = Integer.parseInt(time1.split(":")[0]);
int hour2 = Integer.parseInt(time2.split(":")[0]);
int min1 = Integer.parseInt(time1.split(":")[1]);
int min2 = Integer.parseInt(time2.split(":")[1]);
if(hour1 == 12) {
hour1 = 0;
//System.out.println(hour1);;
}
if(hour2 == 12) {
hour2 = 0;
}
//System.out.println(hour1+" , "+hour2);
if(half1.equals(half2)){
// System.out.println(1);
if(hour1 == hour2){
if(min1 > min2){
return "Second";
}
else{
return "First";
}
}
else if(hour1 > hour2){
return "Second";
}
else{
//System.out.println(2);
return "First";
}
}
else if (half1 == "AM"){
return "First";
}
else{
return "Second";
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
String t1 = in.next();
String t2 = in.next();
String result = timeCompare(t1, t2);
System.out.println(result);
}
}
}
我不河畔é我做錯了什麼。但是,只有十分之一的測試用例通過。
你能說出有什麼問題嗎?
解析爲['LocalTime'](https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html),然後只需調用['compareTo'](https: //docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html#compareTo-java.time.LocalTime-)。 –
'half1 ==「AM」'你不能比較這樣的字符串。 –
@BoristheSpider並非如此。我想通過if語句來做到這一點。 – Nivedita