HackerRank返回空小时的时间转换挑战

b4lqfgs4  于 2022-10-07  发布在  其他
关注(0)|答案(9)|浏览(153)

我尝试在HackerRank上进行Time Conversion挑战,但由于某种原因,24小时表示中的小时字段始终为空。挑战是这样的-
问题陈述

给定AM/PM格式的时间,将其转换为军事(24小时)时间。

注:午夜是12小时制的12:00:00和24小时制的00:00:00。中午是12小时制的中午12:00:00,24小时制的中午12:00:00。

另一个问题似乎是秒总是看起来是1位数。例如,如果输入是07:05:45PM,那么我的HackerRank输出就是:05:4

但是,代码在我桌面上的IntelliJ中运行得很好-

1:24:23AM
1
24
23AM
01:24:23

Process finished with exit code 0

07:05:45PM
07
05
45PM
19:05:45

Process finished with exit code 0

由于无法在HackerRank上调试解决方案本身,我不确定出了什么问题。我的密码是这样的-

package algorithms.Warmup;

import java.util.Scanner;

/**
 * Created by manishgiri on 1/6/16.
 */
public class TimeConversion {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String time = in.next();
        String[] parts = time.split(":");
        for(String part: parts){
            System.out.println(part);
        }
        int hours = Integer.parseInt(parts[0]);
        String minutes = (parts[1]);
        String[] last = parts[2].split("");

        String seconds = last[0]+last[1];
        String timeZ = last[2]+last[3];
        String finalHour = "";
        if(timeZ.equalsIgnoreCase("PM")) {
            if(hours == 12) {
                finalHour = Integer.toString(12);
            }
            else {
                finalHour = Integer.toString(hours + 12);
            }
        }
        else if(timeZ.equalsIgnoreCase("AM")) {
            if(hours == 12) {
                finalHour = "00";
            }
            else if(hours == 10 || hours == 11) {
                finalHour = Integer.toString(hours);
            }
            else {
                finalHour = "0"+hours;
            }
        }
        System.out.println(finalHour+":"+minutes+":"+seconds);

    }
}
jfewjypa

jfewjypa1#

尝试下面的代码。希望这些评论将有助于理解其中的逻辑。

static String timeConversion(String s) {
    // Split the string by :
    String[] sTime = s.split(":");

    int x = 0;

    // if PM and hours >12, add additional 12 to hours
    // for AM and hour = 12, set hour to 00
    if(sTime[sTime.length - 1].contains("PM") && !sTime[0].equals("12"))
        x = 12;

    String val1 = "";
    if(x == 12)
        val1 = (Integer.parseInt(sTime[0]) + x) + "";
    else {
        if(sTime[0].equals("12") && sTime[sTime.length - 1].contains("AM"))
            val1 = "00";
        else
            val1 = sTime[0];
    }

    // merge the string and return the result
    String result = val1 + ":" + sTime[1] + ":" + sTime[2].substring(0,2);
    return result;
}

干杯!

gv8xihay

gv8xihay2#

我不知道为什么您的JRE环境和HackerRank环境之间会有不一致。但如果我们来回答你的问题,在HackerRank中,你的代码将Part[2]分成5个部分,前导是空字符串。

这意味着如果你更新

String seconds = last[0]+last[1];
String timeZ = last[2]+last[3];

AS

String seconds = last[1]+last[2];
String timeZ = last[3]+last[4];

它应该运行,

为了防止初始为空字符串,您可以找到以下一些建议:

How to prevent java.lang.String.split() from creating a leading empty string?

xzlaal3s

xzlaal3s3#

你可以试试HackerRank-

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String time = in.next();
        int hours;
        String hrs = time.substring(0,2);
        hours = Integer.parseInt(hrs);
        String mins = time.substring(3,5);
        String secs = time.substring(6,8);
        String am_pm = time.substring(time.length()-2,time.length());
        if(am_pm.equalsIgnoreCase("PM") && !hrs.equalsIgnoreCase("12")){  
            hours += 12;
            hrs = Integer.toString(hours);
        }
        else if(am_pm.equalsIgnoreCase("AM") && hrs.equalsIgnoreCase("12")){
            hours = 0;
            hrs = "00";
        }
        System.out.println(hrs+":"+mins+":"+secs);
    }
wh6knrhe

wh6knrhe4#

你可以试试这个..。

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String time = in.next();

    String[] sTime = time.split(":");

    int x = sTime[sTime.length - 1].contains("PM") ? 12 : 0 ;

    String val1 = (x == 12) ?  (Integer.parseInt(sTime[0]) + x) + "" :  (sTime[0].equals("12") ? "00" : sTime[0]);

    String result = val1 + ":" + sTime[1] + ":" + sTime[2].substring(0,2);
    System.out.println(result);
}
pbgvytdp

pbgvytdp5#

public static String timecon(String s) {
    System.out.println(s.substring(0, 2));
    int test = Integer.valueOf(s.substring(0, 2));
    if ((s.substring(0, 2).equals("12")) && s.substring(8, 10).equals("AM")) {
        return "00" + ":" + s.substring(3, 8);
    } else if (s.substring(0, 2).equals("12") && (s.substring(8, 10).equals("PM"))) {
        return s.substring(0, 8);
    } else if (s.substring(8, 10).equals("AM")) {
        return s.substring(0, 8);
    }
    else {
        return test + 12 + ":" + s.substring(3, 8);
    }

}
camsedfj

camsedfj6#

可以在Java中尝试进行时间转换HackerRank

/*
 * Complete the timeConversion function below.
 */
static String timeConversion(String s) {
    String h,m,sec;

    h = s.substring(0,2);
    m = s.substring(3,5);
    sec = s.substring(6,8);

    if(s.contains("PM") && !h.equals("12")){
        int inc = 12;
        inc += Integer.parseInt(h);
        h = inc + "";
    }
    if(s.contains("AM") && h.equals("12")){
        h = "00";
    }
    String milTime = h + ":" + m + ":" + sec;
    return milTime;
}
mcdcgff0

mcdcgff07#

试试这个:

public static String timeConversion(String s) {

int h = Integer.parseInt(s.substring(0,2));

if (h == 12) h = 0;

if (s.substring(8,9).equals("P")) h = h + 12;

return String.format("%02d", h) + s.substring(2,8);

}
vi4fp9gy

vi4fp9gy8#

public static string timeConversion(string s)
{
      var idate = DateTime.Parse(s);
      var odate = idate.TimeOfDay;
      return odate.ToString();
}
mhd8tkvw

mhd8tkvw9#

def timeConversion(s):
    # time_r=s[:-2]
    am_pm=s[-2:]
    if am_pm=='PM' and s[:2]!='12':
    s=str(int(s[:2])+12) + s[2:]
    if am_pm=='AM' and s[:2]=='12':
        s="00" + s[2:]
    return s[:-2]

print(timeConversion("07:05:45PM"))

相关问题