题解 | #ranko的手表#
ranko的手表
http://www.nowcoder.com/practice/37275e85ae7c4453920eae6b9f7f45fc
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String startTime = input.nextLine();
String endTime = input.nextLine();
getDuring02(startTime, endTime);
}
//思路一:分别找出startTime 与 endTime的所有可能值
//遍历两个集合,找出min与max
public static void getDuring01(String startTime, String endTime) {
ArrayList<Integer> times01 = new ArrayList<>();
ArrayList<Integer> times02 = new ArrayList<>();
setTimes(times01, startTime);
setTimes(times02, endTime);
int min = 60 * 24 - 1;
int max = 1;
for (int i = 0; i < times02.size(); i++) {
for (int j = 0; j < times01.size(); j++) {
if (times02.get(i) > times01.get(j)) {
min = Math.min(times02.get(i) - times01.get(j), min);
max = Math.max(times02.get(i) - times01.get(j), max);
}
}
}
System.out.println(min + " " + max);
}
private static void setTimes(ArrayList<Integer> times, String time) {
int i = 0;
int max = 60 * 24;
int hour;
int minute;
for (i = 0; i < max; i++) {
hour = i % 60;
minute = i / 60;
if (time.charAt(0) == '?' || time.charAt(0) - '0' == hour % 10) {
if (time.charAt(1) == '?' || time.charAt(1) - '0' == hour / 10) {
if (time.charAt(3) == '?' || time.charAt(3) - '0' == minute % 10) {
if (time.charAt(4) == '?' || time.charAt(4) - '0' == minute / 10) {
times.add(i);
}
}
}
}
}
}
//思路二:startTime与endTime的值确定时应考虑对方,一次性找出需要的解
private static void getDuring02(String startTime, String endTime) {
int max = getMax(startTime, endTime);
int min = getMin(startTime, endTime);
System.out.println(min + " " + max);
}
private static int getMin(String startTime, String endTime) {
char sH1 = startTime.charAt(0);
char sH2 = startTime.charAt(1);
char sM1 = startTime.charAt(3);
char sM2 = startTime.charAt(4);
char eH1 = endTime.charAt(0);
char eH2 = endTime.charAt(1);
char eM1 = endTime.charAt(3);
char eM2 = endTime.charAt(4);
int min = 0;
//确定sH1与eH1
if (sH1 != '?' && eH1 == '?') {
if (sH2 != '?' && eH2 != '?' && eH2 < sH2) {
eH1 = (char)(sH1+1);
} else if (sH2 != '?' && eH2 != '?' && eH2 == sH2) {
if(eM1!='?'&&sM1!='?'&&sM1>eM1){
eH1 = (char)(sH1+1);
}else if(eM1!='?'&&sM1!='?'){
if(eM2!='?'&&sM2!='?'&&sM2>eM2){
eH1 = (char)(sH1+1);
}
}
} else {
eH1 = sH1;
}
} else if (sH1 == '?' && eH1 != '?') {
if (sH2 != '?' && eH2 != '?' && eH2 < sH2) {
sH1 = (char) (eH1-1);
}else if (sH2 != '?' && eH2 != '?' && eH2 == sH2) {
if(eM1!='?'&&sM1!='?'&&sM1>eM1){
sH1 = (char) (eH1-1);
}else if(eM1!='?'&&sM1!='?'){
if(eM2!='?'&&sM2!='?'&&sM2>eM2){
sH1 = (char) (eH1-1);
}
}
}else {
sH1 = eH1;
}
} else if (sH1 == '?') {
if (sH2 != '?' && eH2 != '?' && eH2 < sH2) {
eH1 = '1';
}else {
eH1 = '0';
}
sH1 = '0';
}
//确定sH2与eH2
if(sH2 != '?' && eH2 == '?'){
if(eH1 == sH1){
if(eM1!='?'&&sM1!='?'&&sM1>eM1){
eH2 = (char)(sH2+1);
}else if(eM1!='?'&&sM1!='?'){
if(eM2!='?'&&sM2!='?'&&sM2>eM2){
eH2 = (char)(sH2+1);
}
}else {
eH2 = sH2;
}
}else{
eH2 = '0';
}
} else if (sH2 == '?' && eH2 != '?') {
if(eH1 == sH1){
if(eM1!='?'&&sM1!='?'&&sM1>eM1){
sH2 = (char)(eH2-1);
}else if(eM1!='?'&&sM1!='?'){
if(eM2!='?'&&sM2!='?'&&sM2>eM2){
sH2 = (char)(eH2-1);
}
}else {
sH2 = eH2;
}
}else {
sH2 = '9';
}
} else if (sH2 == '?') {
if(eM1!='?'&&sM1!='?'&&sM1>eM1){
eH2 = '1';
}else if(eM1!='?'&&sM1!='?'){
if(eM2!='?'&&sM2!='?'&&sM2>eM2){
eH2 = '1';
}
}else {
eH2 = '0';
}
sH2 = '0';
}
//确定sM1与eM1
if(eH1==sH1 && eH2==sH2){
if (sM1 != '?' && eM1 == '?') {
if (sM2 != '?' && eM2 != '?' && eM2 < sM2) {
eM1 = (char)(sM1+1);
}else {
eM1 = sM1;
}
} else if (sM1 == '?' && eM1 != '?') {
if (sM2 != '?' && eM2 != '?' && eM2 < sM2) {
sM1 = (char) (eM1-1);
}else {
sM1 = eM1;
}
} else if (sM1 == '?') {
if (sM2 != '?' && eM2 != '?' && eM2 < sM2) {
eM1 = '1';
}else {
eM1 = '0';
}
sM1 = '0';
}
}else {
if(sM1=='?'){
sM1 = '5';
}
if(eM1=='?'){
eM1 = '0';
}
}
//确定sM2与eM2
if(sM2 != '?' && eM2 == '?'){
if(eM1 == sM1){
eM2 = sM2;
}else {
eM2 = '0';
}
} else if (sM2 == '?' && eM2 != '?') {
if(eM1 == sM1){
sM2 = eM2;
}else {
sM2 = '9';
}
} else if (sM2 == '?') {
if(sH1==eH1 && sH2 == eH2 && sM1 == eM1){
sM2 = '0';
}else {
sM2 = '9';
}
eM2 = '0';
}
int time1 = Integer.parseInt(""+sH1+sH2)*60 + Integer.parseInt(""+sM1+sM2);
int time2 = Integer.parseInt(""+eH1+eH2)*60 + Integer.parseInt(""+eM1+eM2);
min = time2 -time1;
return min==0?1:min;
}
private static int getMax(String startTime, String endTime) {
char sH1 = startTime.charAt(0);
char sH2 = startTime.charAt(1);
char sM1 = startTime.charAt(3);
char sM2 = startTime.charAt(4);
char eH1 = endTime.charAt(0);
char eH2 = endTime.charAt(1);
char eM1 = endTime.charAt(3);
char eM2 = endTime.charAt(4);
int max = 0;
//确定sH1与eH1
if (sH1 != '?' && eH1 == '?') {
if(eH2!='?'&&eH2>'3'){
eH1 = '1';
}else {
eH1 = '2';
}
} else if (sH1 == '?' && eH1 != '?') {
sH1 = '0';
} else if (sH1 == '?') {
if (eH2!='?'&&eH2>'3') {
eH1 = '1';
}else {
eH1 = '2';
}
sH1 = '0';
}
//确定sH2与eH2
if(sH2 != '?' && eH2 == '?'){
if(eH1 == '2'){
eH2 = '3';
}else {
eH2 = '9';
}
} else if (sH2 == '?' && eH2 != '?') {
sH2 = '0';
} else if (sH2 == '?') {
sH2 = '0';
if(eH1 == '2'){
eH2 = '3';
}else {
eH2 = '9';
}
}
//确定sM1与eM1
if(sM1 == '?'){
sM1 = '0';
}
if(eM1 == '?'){
eM1 = '5';
}
//确定sM2与eM2
if(sM2 == '?'){
sM2 = '0';
}
if(eM2 == '?'){
eM2 = '9';
}
int time1 = Integer.parseInt(""+sH1+sH2)*60 + Integer.parseInt(""+sM1+sM2);
int time2 = Integer.parseInt(""+eH1+eH2)*60 + Integer.parseInt(""+eM1+eM2);
max = time2 -time1;
return max;
}
}