zoukankan      html  css  js  c++  java
  • js矩阵

    螺旋矩阵:

    export default(arr)=>{
      //处理每一圈的数据遍历过程
      let map=(arr,r=[])=>{
        for(let i=0,len=arr.length;i<len;i++){
          if(i===0){
            r=r.concat(arr[i])
          }else if(i===len-1){
            r=r.concat(arr[i],reverse())
          }else{
            r.push(arr[i].pop())
          }
        }
        arr.shift()
        arr.pop()
        for(let i=arr.length-1;i>=0;i--){
           r.push(arr[i].shift())
        }
        if(arr.length){
          return map(arr,r)
        }else{
          return r
        }
      }
      return map(arr,[])
    }
    

      

    旋转图像:

     

     

    export default(arr)=>{
      // 获取n的维度
      let vecor=arr.length
      //垂直反转
      for(let i=0,len=vecor/2;i<len;i++){
        for(let j=0,tmp;j<vecor;j++){
           tmp=arr[i][j]
           arr[i][j]=arr[vecor-i-1][j]
           arr[vecor-i-1][j]=tmp
        }
      }
      //对角线翻转
      for(let i=0;i<vecor;i++){
        for(let j=0,tmp;j<i;j++){
          tmp=arr[i][j]
          arr[i][j]=arr[j][i]
          arr[j][i]=tmp
        }
      }
      return arr
    }
    

      二叉树:

    对称的二叉树:

     

      //二叉树的节点
      class Node{
        constructor(val){
          this.val=val
          this.left=this.right=undefined
        }
      }
      class Tree{
        constructor(data){
          //临时存储所有节点,方便寻找父子节点
          let nodeList=[]
          //顶节点
          let root
          for(let i=0,len=data.length;i<len;i++){
            let node=new Node(data[i])
            nodeList.push(node)
            if(i>0){  //第一层
              //计算当前节点属于哪一层
              let n=Math.floor( Math.sqrt(i+1))
              //记录当前层的起始点
              let q=Math.pow(2,n)-1
              //记录上一层的起始点
              let p=Math.pow(2,n-1)-1
              //找到当前节点的父节点
              let parent=nodeList[p+Math.floor((i-q)/2)]
              //将当前节点和上一层的父节点做关联
              if(parent.left){
                parent.right=node
              }else{
                parent.left=node
              }
            }
          }
          root=nodeList.shift()
          nodeList.length=0 
          return root
        }
        //判断是不是对称
        static isSymmetry(root){
          if(!root){
            return true
          }
          let walk=(left,right)=>{
            if(!left&&!right){
              return true
            }
            if((left&&!right)||(!left&&right)||(left.val!==right.val)){
              return false
            }
            return walk(left.left,right.right)&&walk(left.right,right.left)
          }
          return walk(root.left,root.right)
        }
      }
    
      export default Tree
    
      export{
        Node
      }
    

      

    class Node{
      constructor(val){
        this.val=val
        this.left=this.right=undefined
      }
    }
    
    class Tree{
      constructor(data){
        let root=new Node(data.shift())
        //遍历所有的数据,逐渐插入到当前这颗搜索树中
        data.array.forEach(item => {
          this.insert(root,item)
        });
        return root
      }
      insert(node,data){
        if(node.val>data){
          if(node.left===undefined){//判断是不是左节点
            node.left=new Node(data)
          }else{
            this.insert(node.left,data)
          }
        }else{
          if(node.right===undefined){
            node.right=new Node(data)
          }else{
            this.insert(node.right,data)
          }
        }
      }
    
      static walk(root){
        if(!root.left&&!root.right){
          return true
        }else if((root.left&&root.val<root.left.val)||
        root.right&&root.val>root.right.val){
          return false
        }else{
          return Tree.walk(root.left)&&Tree.walk(root.right)
        }
      }
    }
    
    export default Tree
    export{
      Node
    }
    

      

  • 相关阅读:
    配置FTP服务2(vsftpd 配置虚拟账号)
    配置FTP服务(pure-ftpd安装配置测试)
    asp.net学习——Repeater控件
    C#方法,属性,和事件
    (转)AspNetPager使用方法
    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
    SQL Server 操作数据
    SQL Server——增、删、改、查。
    数组——求和、平均分、最值
    数组——抽奖&&句子组合
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/13526182.html
Copyright © 2011-2022 走看看