java-多节点的上下移解决方案

import java.util.*;

/**
 * @Author 59456
 * @Date 2021/7/24
 * @Descrip 解决离散或者相邻部门上下的问题,时间复杂度O(N)、空间复杂度O(1)
 * @Version 1.0
 */
public class Solution {

    static Short MOVE_UP = 1;
    static Short MOVE_DOWN = 0;
    static List<Long> idList = new ArrayList<>();
    static Set<Long> idSet = new HashSet<>();
    static List<DepartmentMoveDto> departmentMoveDtoList = new ArrayList<>();

    static {
        // 设置原始有序数据集
        for(Long i=0L;i<10L;i++){
            DepartmentMoveDto departmentMoveDto = new DepartmentMoveDto();
            departmentMoveDto.setId(i);
            departmentMoveDto.setSort(Short.valueOf(i.toString()));
            departmentMoveDto.setSortIndex(Double.valueOf(i.toString()));
            departmentMoveDtoList.add(departmentMoveDto);
        }

        // 设置需要移动的节点id
        // 其中0、1是一个空气泡,5、6是另外一个空气泡
        idList.add(0L);
        idList.add(1L);
        idList.add(5L);
        idList.add(6L);
        idList.add(9L);

    }


    // 逆向思考:另外的节点移动
    public static void move(List<Long> idList,Short moveType,Long parentId){
        if(null == idList || idList.isEmpty()){
            return;
        }

        // 获取同级部门排序信息
        //        List<DepartmentMoveDto> departmentMoveDtoList = departmentDao.getDepartmentMoveDtoListByParentId(parentId);
        idSet = new HashSet<>(idList);

        if(MOVE_UP.equals(moveType)){
            moveUp(departmentMoveDtoList ,idSet);
        }else {
            Collections.reverse(departmentMoveDtoList);
            moveUp(departmentMoveDtoList ,idSet);
            Collections.reverse(departmentMoveDtoList);
        }


    }

    /**
     * 专门针对上移设计(其实下移也是另外一个视角的上移)
     * 核心思想聚焦需要移动节点,上方相邻的普通节点(本质也可以看做这些节点的向下冒泡)
     * @param departmentMoveDtoList 部门移动信息
     * @param idSet 需要移动的部门id
     */
    private static void moveUp(List<DepartmentMoveDto> departmentMoveDtoList,Set<Long> idSet){
        int length = departmentMoveDtoList.size();
        for(int index = 0; index<length-1; index++){
            DepartmentMoveDto departmentMoveDtoPre = departmentMoveDtoList.get(index);
            Long idPre = departmentMoveDtoPre.getId();

            DepartmentMoveDto departmentMoveDtoNext = departmentMoveDtoList.get(index+1);
            Long idNext = departmentMoveDtoNext.getId();

            // 如果当前不移动,下一个移动:交换
            if (!idSet.contains(idPre) && idSet.contains(idNext)) {
                DepartmentMoveDto departmentMoveDtoSwitch = departmentMoveDtoList.get(index);
                departmentMoveDtoList.set(index, departmentMoveDtoList.get(index + 1));
                departmentMoveDtoList.set(index + 1,departmentMoveDtoSwitch);
            }
        }
    }


    public static void main(String[] args) {
        departmentMoveDtoList.forEach(entity->{
            System.out.println(entity.toString());
        });
        System.out.println("hello world!");

        // 部门下移
        move( idList, new Short("0"), 0L);

        departmentMoveDtoList.forEach(entity->{
            System.out.println(entity.toString());
        });
    }

}

上一篇:正则表达式匹配:关于引号的问题


下一篇:多线程(四)锁