2020-11-23

 public static int getMinTransform(String str1,String str2)
    {
        if (null == str1 || null == str2) return -1;
        if (str1.length() != str2.length()) return -1;
        if (str1.equals(str2)) return 0;

        int n = str1.length(), size1 = 0, size2 = 0, p = 0, q = 0;
        boolean[] v1 = new boolean[30];
        boolean[] v2 = new boolean[30];
        for (int i = 0; i < n; i++) {
            p = str1.charAt(i) - 'a';
            q = str2.charAt(i) - 'a';
            if (!v1[p]) {
                size1++;
                v1[p]=true;
            }
            if (!v2[q]) {
                size2++;
                v2[q]=true;
            }
        }
        if (size2 == 26 || size1 < size2) return -1;
        char[] s1ToS2 = new char[30];

        char c = '.';
        for (int i = 0; i < 26; i++) s1ToS2[i] = '.';
        for (int i = 0; i < n; i++) {
            p = str1.charAt(i) - 'a';
            c = str2.charAt(i);
            if (s1ToS2[p] == '.' || s1ToS2[p] == c) {
                s1ToS2[p] = c;
            }
            else return -1;
        }
        int[] cnt = new int[28];
        for(int i=0;i<26;++i){
            if(s1ToS2[i]=='.') continue;
            int x = s1ToS2[i]-'a';
            //System.out.println(s1ToS2[i]-'a');
            cnt[x]++;
        }
        int ans=0;
        for(int i=0;i<26;++i) {
            ans+=cnt[i];
        }
        int tmpans=0;
        for(int i=0;i<26;++i){
            if(s1ToS2[i]!='.'){
               // System.out.println(s1ToS2[s1ToS2[i]-'a']);
                //System.out.println(i+'a');
                if(s1ToS2[s1ToS2[i]-'a'] == i+'a'){
                    tmpans++;
                }
            }
        }
        ans+=tmpans/2;
        System.out.println(ans);
        return ans;
    }

 

可以使用Java中的SimpleDateFormat类将String类型的时间转换为Date对象,然后再对Date对象进行处理。 首先,我们先将时间段转换为【2020-11-20 00:00:00,2021-10-09 23:59:59】,即将结束时间改为当天的最后一秒: ```java String startTime = "2020-11-20 09:09:09"; String endTime = "2021-10-09 10:10:10"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = sdf.parse(startTime); Date endDate = sdf.parse(endTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); endCalendar.set(Calendar.HOUR_OF_DAY, 23); endCalendar.set(Calendar.MINUTE, 59); endCalendar.set(Calendar.SECOND, 59); endDate = endCalendar.getTime(); String newEndTime = sdf.format(endDate); System.out.println(newEndTime); ``` 输出结果为:2021-10-09 23:59:59 接下来,我们可以使用一个循环,每次增加一天,输出该天的开始时间和结束时间: ```java Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); while (calendar.getTime().before(endDate)) { Date startOfDay = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date endOfDay = calendar.getTime(); System.out.println(sdf.format(startOfDay) + " - " + sdf.format(endOfDay)); } // 输出最后一天的开始时间和结束时间 Date startOfDay = calendar.getTime(); System.out.println(sdf.format(startOfDay) + " - " + newEndTime); ``` 输出结果为: ``` 2020-11-20 09:09:09 - 2020-11-21 00:00:00 2020-11-21 00:00:00 - 2020-11-22 00:00:00 2020-11-22 00:00:00 - 2020-11-23 00:00:00 ... 2021-10-07 00:00:00 - 2021-10-08 00:00:00 2021-10-08 00:00:00 - 2021-10-09 00:00:00 2021-10-09 00:00:00 - 2021-10-09 23:59:59 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值