[HackerRank] Time Conversion

原创 2016年05月31日 06:51:46

Problem

Given a time in AM/PM format, convert it to military (24-hour) time.

Input Format

A single string containing a time in 12-hour clock format.

Output Format

Convert and print the given time in 24-hour format.

Sample Input

07:05:45PM

Sample Output

19:05:45

Solution

1. using char[]

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char[] ch = str.toCharArray();
        char[] res = Arrays.copyOfRange(ch, 0, 8);

        if(ch[8] == 'A' && str.substring(0,2).equals("12")) {
            res[0] = '0';
            res[1] = '0';
        }
        else if(ch[8] =='P' && !str.substring(0,2).equals("12")) {
            String hour = "" + (Integer.parseInt(str.substring(0,2)) + 12);
            char[] h = hour.toCharArray();
            res[0] = h[0];
            res[1] = h[1];
        }
        System.out.println(res);
    }
}

2. using StringBuilder()

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char[] inChar = str.toCharArray();
        StringBuilder sb = new StringBuilder();

        if(str.charAt(8) == 'A' && str.substring(0,2).equals("12")) {
            sb.append("00");
            sb.append(str.substring(2, 8));
        }
        else if(str.charAt(8) =='P' && !str.substring(0,2).equals("12")) {

            String s = "" + (Integer.parseInt(str.substring(0,2)) + 12);
            sb.append(s);
            sb.append(str.substring(2, 8));
        }
        else sb.append(str.substring(0, 8));
        System.out.println(sb.toString());
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HackerRank Weekly Challenges - Week 6: Minimum Average Waiting Time 停不下来的厨师

原题链接:Minimum Average Waiting Time 题目大意:

pyschools中的12hr To 24hr Time Conversion

>>> time24hr('12:34am') '0034hr' >>> time24hr('12:15pm') '1215hr'#下午时间有可能是‘1:45pm’所以要对根据字符串...

【HackerRank】Common Child (LCS)最长公共子序列

Given two strings a and b of equal length, what’s the longest string (S) that can be constructed suc...

hackerrank challenges median

题目链接 Median dynamic Max Score: 67 The median of M numbers is defined as the ...

【Hackerrank】Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool You’re given the pointer to the head n...

HackerRank Self Balancing Tree(AVL树)

题目链接 /* Node is defined as : typedef struct node { int val; struct node* left; struct n...

[HackerRank Week of Code]Jogging Cats/[JZOJ5037]轮回

题目大意给定一个nn个点,mm条边的无重边和自环的无向图,求图中不同的四元环个数。1≤n≤5×104,1≤m≤1051\le n\le5\times10^4,1\le m\le10^5题目分析首先我们...

Hackerrank Sequential Prefix Function

题目链接:https://www.hackerrank.com/contests/w22/challenges/sequential-prefix-function 这竟然是一道水题,当时打比赛时竟...

hackerrank>Dashboard>C++>STL>Vector-Erase

You are given a vector of integers.Then you are given queries.First query consists of integer ...

[hackerrank w25]DAG Queries 解题报告

又用bitset强上了一道题好开心啊~(然而这题标算似乎就是bitset?)如果暴力的话,可以用bitset,但是空间爆炸;所以我们先解决一下空间问题。 如果考虑对操作分块的话,我们可以只用处理所有...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)