【实战总结】日期排序

1、单链表实现

public class LinkedList { 
    private final Integer[] week; 
   
    public LinkedList() { 
        week = new Integer[]{1, 2, 3, 4, 5, 6, 7};//一周中的7天 
    } 
   
    public static Integer[] orderByWeekDayWithLinkedList(Integer[] week, int day) { 
        if (day < 1 || day > 7) throw new IllegalArgumentException(); 
        if (1 == day) return week; 
        //数组转单链表 
        java.util.LinkedList<Integer> linkedList = new java.util.LinkedList(Arrays.asList(week)); 
        java.util.LinkedList<Integer> restructureList = new java.util.LinkedList(); 
        final int seperate_index = day - 1; 
        final int last_index = week.length; 
   
        //截取0到指定位置之间的集合(不包含指定位置的元素) 
        List<Integer> lastList = linkedList.subList(0, day - 1); 
        //截取0到指定位置之间的集合(不包含指定位置的元素) 
        List<Integer> headList = linkedList.subList(seperate_index, last_index); 
        //重新组合 
        restructureList.addAll(headList); 
        restructureList.addAll(lastList); 
   
        for (int i = 0; i < restructureList.size(); i++) { 
            week[i] = restructureList.get(i); 
        } 
        return week; 
    } 
   
    public static void main(String[] args) { 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 1))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 2))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 3))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 4))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 5))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 6))); 
        System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 7))); 
    } 
}

2、求逆算法实现

public class Reverse { 
    private final Integer[] week; 
   
    public Reverse() { 
        week = new Integer[]{1, 2, 3, 4, 5, 6, 7};//一周中的7天 
    } 
   
    /**
     * 求逆算法
     * <p>
     * 从ab开始,先转置a,得到序列a┐b
     * 再转置b,得到序列a┐b┐。
     * 最后对转置后的序列进行转置(a┐b┐)┐,得到序列ba
     *
     * @param week 一周天数
     * @param day  指定哪天为第一,周一:1
     */ 
    public static Integer[] orderByWeekDayWithInverse(Integer[] week, int day) { 
        if (day < 0 || day > 7) throw new IllegalArgumentException(); 
        //1234567  ab 
        //1237654  ab┐ 
        //3217654  a┐b┐ 
        //4567123  ba 
        if (day == 1) return week; 
   
        final int separate_index = day - 2;// -1:为了天数适配索引,所以偏移1; -1:为了翻转保持输入数字作为当前位在首位,所以又偏移1 
        final int last_index = week.length - 1;//索引最大值 
        final int reverse_index = last_index - (last_index - separate_index) + 1;//第二次翻转数组的起点 
   
        handle(week); 
        //ab => a┐b 
        reverse(week, 0, separate_index); 
        //a┐b => a┐b┐ 
        reverse(week, reverse_index, last_index); 
        //a┐b┐ => ba 
        reverse(week, 0, last_index); 
        return week; 
    } 
   
    private static Integer[] handle(Integer[] i) { 
        if (null == i || 0 == i.length) return null; 
        return i; 
    } 
   
    /**
     * 翻转数组方法
     *
     * @param arr  数组
     * @param from 开始索引
     * @param to   结束索引
     * @return
     */ 
    private static Integer[] reverse(Integer[] arr, int from, int to) { 
        //System.out.println(String.format("arr:%s from:%s to:%s", Arrays.toString(arr), from, to)); 
        if (null == arr || 0 == arr.length 
                || from < 0 || from > arr.length 
                || to < 0 || to > arr.length) 
            throw new IllegalArgumentException(); 
   
        int tmp; 
        while (from < to) { 
            tmp = arr[to]; 
            arr[to] = arr[from]; 
            arr[from] = tmp; 
            from++; 
            to--; 
        } 
        return arr; 
    } 
   
    public static void main(String[] args) { 
        Integer[] Monday = orderByWeekDayWithInverse(new Reverse().week, 1); 
        System.out.println(Arrays.toString(Monday)); 
        Integer[] Tuesday = orderByWeekDayWithInverse(new Reverse().week, 2); 
        System.out.println(Arrays.toString(Tuesday)); 
        Integer[] Wednesday = orderByWeekDayWithInverse(new Reverse().week, 3); 
        System.out.println(Arrays.toString(Wednesday)); 
        Integer[] Thursday = orderByWeekDayWithInverse(new Reverse().week, 4); 
        System.out.println(Arrays.toString(Thursday)); 
        Integer[] Friday = orderByWeekDayWithInverse(new Reverse().week, 5); 
        System.out.println(Arrays.toString(Friday)); 
        Integer[] Saturday = orderByWeekDayWithInverse(new Reverse().week, 6); 
        System.out.println(Arrays.toString(Saturday)); 
        Integer[] Sunday = orderByWeekDayWithInverse(new Reverse().week, 7); 
        System.out.println(Arrays.toString(Sunday)); 
    } 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大摩羯先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值