zoukankan      html  css  js  c++  java
  • 腾讯-前端特工之秘密潜入五

    入口:http://codestar.alloyteam.com/1/5

    潜入工具:

     1 (function(window){
     2     var 
     3     //初始化最大值
     4     max = 0, 
     5     //记录遍历结点的值
     6     levelPath = [],
     7     
     8     finalKeyPathStr = '',
     9     
    10     ShortPathVal = window.ShortPathVal= function(dataArr){
    11         trvaseGraph(createGraph(dataArr),max);
    12         return finalKeyPathStr;
    13     }
    14     
    15     //邻接表的形式创建无向图
    16     function createGraph(arr){
    17         var 
    18         //头结点
    19         head={},
    20         //父结点的数组,每个父节点都包括左/右子结点
    21         lastLevelObjects=[];
    22         for(var i=0;i<arr.length;i++){
    23             var  newArr= arr[i].split(","),
    24             levelObjects=[];
    25             for(var j=0;j<newArr.length;j++){
    26                 var o={};
    27                 o.value = newArr[j];
    28                 o.level = i+1;
    29                 levelObjects.push(o);
    30                 if(j===0 && i===0){
    31                     //设置头结点
    32                     head=o;
    33                     break;
    34                 }
    35                 if(j-1>=0)
    36                 //设置右边的子结点
    37                 lastLevelObjects[j-1].rightObject=o;
    38                 if(j<lastLevelObjects.length)
    39                 //设置左边的子结点
    40                 lastLevelObjects[j].leftObject=o;
    41             }
    42             //重置lastLevelObjects为对象(当前的level属性的值)数组
    43             lastLevelObjects=levelObjects;
    44         }
    45         return head;
    46     }
    47     //递归遍历图找出求得最大值的路线
    48     function trvaseGraph(start,currentValue){
    49         //保证levelPath的长度小于当前结点的level属性值,避免多余的“脏”数据
    50         while(levelPath.length>=parseInt(start.level)) levelPath.pop();
    51         
    52         levelPath.push(start.value);
    53         
    54         if(start.leftObject==null && start.rightObject==null){ 
    55             //在当前遍历中遍历到最后一层子结点的value和值
    56             currentValue +=parseInt(start.value);
    57             if(max<currentValue){
    58                 var route="";
    59                 
    60                 //重置max
    61                 max=currentValue;
    62                 for(var i=levelPath.length-2;i>=0;i--){
    63                     route=levelPath[i]+'-->'+route;
    64                 }
    65                 route+=levelPath[levelPath.length-1];
    66                 finalKeyPathStr="Path:"+route+"  MaxValue:"+max;
    67             }
    68             return;
    69         }
    70         //在当前遍历中未遍历到最后一层子结点的value和值
    71         currentValue+=parseInt(start.value);
    72         if(start.leftObject!=null){
    73             //遍历左边子结点
    74             trvaseGraph(start.leftObject,currentValue);
    75         }
    76         
    77         if(start.rightObject!=null){
    78             //遍历右边子结点
    79             trvaseGraph(start.rightObject,currentValue);
    80         }
    81     }
    82     
    83 })(window)
    84 
    85 var dataArr =["188",
    86         "188,112",
    87         "105,105,194",
    88         "142,166,191,129",
    89         "171,172,134,127,127",
    90         "131,152,160,162,165,134",
    91         "102,167,102,180,125,153,126",
    92         "111,122,131,188,136,125,190,109",
    93         "168,160,188,126,164,175,181,128,163",
    94         "114,148,121,111,162,102,151,182,134,186",
    95         "126,102,166,183,161,174,159,187,113,198,159",
    96         "191,143,196,186,156,103,112,124,200,140,183,124"];
    97 console.log(ShortPathVal(dataArr));
    View Code

    潜入地图:


    成功潜入!!!

    DEMO:http://jsfiddle.net/seamar/QtRTb/2/

  • 相关阅读:
    javascript
    js中 filter()方法
    DOM 属性方法用法
    zabbix-4.0-如何监控windows服务和实战监控SQL SERVER数据
    路由交换-华为交换机查询MAC/ARP配置电脑地址实战
    杀毒软件-使用杀毒软件带的防火墙设置端口访问控制-保护终端安全
    windows-win10各版本升级密钥
    等保心得经验总结
    Emacs服务器模式以及emacsclient配置
    PyTorch自动微分基本原理
  • 原文地址:https://www.cnblogs.com/seamar/p/3402750.html
Copyright © 2011-2022 走看看