zoukankan      html  css  js  c++  java
  • 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());
            });
        }
    
    }
    
    
    探究未知是最大乐趣
  • 相关阅读:
    时序点过程学习笔记
    蚂蚁集团 CeresDB 团队 | Rust CPU Affinity 初探
    宽客Quant量化投资书籍推荐(33本)
    线性代数库调研
    比较OpenBLAS,Intel MKL和Eigen的矩阵相乘性能
    空间点过程&点格局分析
    4-网络芯片CH395Q学习开发-关于中断检测和DHCP实验
    3-网络芯片CH395Q学习开发-芯片初始化,网线连接检测实验(轮训和中断方式)
    2-网络芯片CH395Q学习开发-学习资料说明,测试通信,获取硬件版本,获取设备本身MAC,代码移植说明
    001-STM32+Air724UG基本控制篇(华为云物联网平台)--测试STM32+Air724UG(4G模组),Android,微信小程序等连接华为云物联网平台
  • 原文地址:https://www.cnblogs.com/Mufasa/p/15056939.html
Copyright © 2011-2022 走看看