華爲機試JAVA 免單統計

免單統計 
題目描述 
某商城舉辦了一個促銷活動,如果某顧客是某一秒內早時刻下單的顧客(可能是多個人),則可以 獲取免單。請你編程計算有多少顧客可以獲取免單。
解答要求 時間限制:3000ms, 內存限制:64MB 輸入 輸入爲n行數據,每一行表示一位顧客的下單時 間。 以(年-月-日 時-分-秒.毫秒)yyyy-MM-dd HH:mm:ss.fff形式給出。

0<n<50000 2000<yyyy<2020 0<MM<=12 0<dd<=28 0<=HH<=23 0<=mm<=59 0<=ss<=59 0<=fff<=999 所有輸 入保證合法。
輸出 輸出一個整數,表示有多少顧客可以獲取免單。
樣例:
輸入樣例1
2019-01-01 00:00:00.001 
2019-01-01 00:00:00.002 
2019-01-01 00:00:00.003
輸出樣例1 

輸入樣例2
2019-01-01 08:59:00.123 
2019-01-01 08:59:00.123
2018-12-28 10:08:00.999 
輸出樣例2 
3
 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 輸入格式:yyyy-MM-dd HH:mm:ss.fff
 * 2019-01-01 08:59:00.123
 * 2019-01-01 08:59:00.123
 * 2018-12-28 10:08:00.999
 *
 * 輸出:3
 *
 * 輸入格式:yyyy-MM-dd HH:mm:ss.fff
 * 2019-01-01 08:59:00.123
 * 2019-01-01 08:59:00.123
 *
 * 輸出:2
 */
public class MainClass {
    private static Integer n = 50000;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> topDates = new ArrayList<>();
        int i = 0;
        while (in.hasNextLine()){
            String orderDate = in.nextLine();
            if(validateDate(orderDate)){
                topDates.add(orderDate);
            }
            i++;
            if(i>=n)break;
        }
        Integer topUserNum =  doGetTopUserNum(topDates);
        System.out.println(topUserNum);
        in.close();
    }

    public static Integer doGetTopUserNum(List<String> topDates){
        Integer topUser = 0;
        Map<String,String> map = new HashMap<>();
        if(topDates !=null &&topDates.size()!=0){
            for (String topDate:
                    topDates) {
                String[] dateAndSec = topDate.split("\\.");
                String pre = dateAndSec[0];
                String suf = dateAndSec[1];
                putMinValueToMap(pre,suf,map);
            }

            for (Map.Entry<String,String> entry:
                    map.entrySet()){
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(entry.getKey()).append(".").append(entry.getValue());

                for (int i = 0; i < topDates.size(); i++) {
                    if(stringBuffer.toString().equals(topDates.get(i))){
                        topUser++;
                    }
                }
            }

        }
        return topUser;
    }

    public static boolean validateDate(String date){
        Boolean validate = true ;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            simpleDateFormat.parse(date);
        } catch (ParseException e) {
            validate = false;
        }
        return validate;

    }

    /**
     * 用map放取最小數據
     * @param datePreFix
     * @param datesufFix
     * @param map
     */
    public static void putMinValueToMap(String datePreFix,String datesufFix,Map<String,String> map){
        Integer value = map.get(datePreFix)==null?null:Integer.valueOf(map.get(datePreFix));
        if(value==null){
            map.put(datePreFix,datesufFix);
        }else if(value.intValue() > Integer.valueOf(datesufFix).intValue()){
            map.put(datePreFix,datesufFix);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章