leetcode681

在这里插入图片描述
暴力解法

public class test {
    public String nextClosedTime(String s){
        int[] a = new int[4];
        int[][] result = new int[256][4];
        int[] time1 = new int[256];
        int count = 0;
        int index = Integer.MAX_VALUE;
        int resultIndex = Integer.MAX_VALUE;
        String p = "";

        a[0] = s.charAt(0) - '0';
        a[1] = s.charAt(1) - '0';
        a[2] = s.charAt(3) - '0';
        a[3] = s.charAt(4) - '0';

        /**
         * 列举所有可能性
         */
        for(int i=0; i<4; i++){
            for(int j=0; j<4; j++){
                for(int k=0; k<4; k++){
                    for(int t=0; t<4; t++){
                        result[count][0] = a[i];
                        result[count][1] = a[j];
                        result[count][2] = a[k];
                        result[count][3] = a[t];
                        count++;
                    }
                }
            }
        }

        /**
         * 清除不合理的数值
         */
        for(int i=0; i<256; i++){
            if(result[i][0] > 2 || (result[i][0]==2 && result[i][1]>3) || result[i][2]>5){
                result[i][0] = Integer.MAX_VALUE;
                result[i][1] = Integer.MAX_VALUE;
                result[i][2] = Integer.MAX_VALUE;
                result[i][3] = Integer.MAX_VALUE;
            }
        }

        /**
         * 记录时间
         */
        for(int i=0; i<256; i++){
            if(result[i][0] == Integer.MAX_VALUE){
                time1[i] = Integer.MAX_VALUE;
            }else{
                time1[i] = result[i][0]*60*10 + result[i][1]*60 +result[i][2]*10 + result[i][3];
                time1[i] = time1[i] - (a[0]*60*10 + a[1]*60 + a[2]*10 + a[3]);
                if(time1[i] <= 0){
                    time1[i] += 24*60;
                }
            }
        }

        /**
         * 获取最小时间差
         */
        for(int i=0; i<256; i++){
            if(time1[i] < resultIndex){
                resultIndex = time1[i];
                index = i;
            }
        }

        if(index < 256){
            p += result[index][0] + result[index][1] + ":" + result[index][2] + result[index][3];
        }
        return p;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值