笔试题五道

作者: yellowone | 来源:发表于2020-07-10 16:09 被阅读0次

笔试题五道

只能说自己抗压能力实在是太一般了。。

反转链表:

import "fmt"

//输入: 1->2->3->4->5->NULL
//输出: 5->4->3->2->1->NULL

type ListNode struct {
    Val  int
    Next *ListNode
}

func main() {
    fmt.Printf("%+v",ReversalList(&ListNode{
        Val:  1,
        Next: &ListNode{
            Val:  2,
            Next: &ListNode{
                Val:  3,
                Next: nil,
            },
        },
    }))
}
func ReversalList(listNode *ListNode) *ListNode {
   if listNode == nil {
      return nil
   }
   preNode := new(ListNode)
   nowNode := listNode
   nextNode := new(ListNode)
   for nowNode != nil {
      nextNode = nowNode.Next
      nowNode.Next = preNode
      preNode = nowNode
      nowNode = nextNode
   }
   return preNode
}

合并数组

//输入:
//A = [1,2,3,0,0,0], m = 3
//B = [2,5,6],       n = 3
//输出: [1,2,2,3,5,6]

func main() {
   fmt.Printf("%+v", getNewArray([]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3))
}

//从尾往前排序
func getNewArray(a []int, m int, b []int, n int) []int {
   if len(a) < m+n {
      return make([]int, 0)
   }
   nowIndex := m + n -1
   for n-1 >= 0 {
      if b[n-1] >= a[m-1] {
         a[nowIndex] = b[n-1]
         n--
      } else {
         //将a往后移动到指定位置
         a[m-1], a[nowIndex] = a[nowIndex], a[m-1]
         m--
      }
      nowIndex--
   }
   return a
}

橘子腐烂:

package main

import "fmt"

func main() {

    fmt.Printf("%d",orangesRotting([][]int{
        {2, 1, 0}, {1, 1, 0}, {0, 1, 1},
    }))
}

func orangesRotting(grid [][]int) int {
    if len(grid) <= 0 || len(grid[0]) <= 0 {
        return 0
    }
    listRotting := make([]int, 0)
    rottingMap := make(map[int]int)
    row := len(grid)
    col := len(grid[0])
    //遍历数组,找出那些腐烂的橘子,入队列进行广度搜索
    for i := 0; i < row; i++ {
        for j := 0; j < col; j++ {
            if grid[i][j] == 2 {
                listRotting = append(listRotting, i*col+j)
                rottingMap[i*col+j] = 0
            }
        }
    }
    //用来定位上下左右
    dc := []int{-1, 0, 1, 0}
    dr := []int{0, 1, 0, -1}
    walk := -1
    for len(listRotting) > 0 {
        rot := listRotting[0]
        listRotting = listRotting[1:]
        i := rot / col
        j := rot % col
        for k := 0; k < 4; k++ {
            checkI := i + dr[k]
            checkJ := j + dc[k]
            if checkI >= 0 && checkJ >= 0 && checkI < row && checkJ < col && grid[checkI][checkJ] == 1 {
                grid[checkI][checkJ] = 2
                checkRoute := checkI*col + checkJ
                listRotting = append(listRotting, checkRoute)
                rottingMap[checkRoute] = rottingMap[rot] + 1
                if walk < rottingMap[checkRoute] {
                    walk = rottingMap[checkRoute]
                }
            }
        }
    }
    for i := 0; i < row; i++ {
        for j := 0; j < col; j++ {
            if grid[i][j] == 1 {
                return -1
            }
        }
    }
    return walk
}

能获取最大值的队列

type MaxQueue struct {
    MaxList []int
    List    []int
}

func Constructor() MaxQueue {
    return MaxQueue{
        MaxList: make([]int, 0),
        List:    make([]int, 0),
    }
}

func (this *MaxQueue) MaxValue() int {
    if len(this.MaxList) <= 0 {
        return -1
    }
    return this.MaxList[0]
}

func (this *MaxQueue) PushBack(value int) {
    for len(this.MaxList) > 0 && this.MaxList[len(this.MaxList)-1] < value {
        this.MaxList = this.MaxList[:len(this.MaxList)-1]
    }
    this.MaxList = append(this.MaxList, value)
    this.List = append(this.List, value)
}

func (this *MaxQueue) PopFront() int {
    if len(this.List) <= 0 {
        return -1
    }
    front := this.List[0]
    this.List = this.List[1:]
    if front == this.MaxList[0] {
        this.MaxList = this.MaxList[1:]
    }
    return front
}

第五题,树的深度

func MaxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    return maxDepth(root,1)
}

func maxDepth(root *TreeNode, nowDepth int) int {
    if root == nil {
        return nowDepth
    }
    return max(maxDepth(root.Left, nowDepth+1), maxDepth(root.Right, nowDepth+1))
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

相关文章

  • 笔试题五道

    笔试题五道 只能说自己抗压能力实在是太一般了。。 反转链表: 合并数组 橘子腐烂: 能获取最大值的队列 第五题,树的深度

  • 面试题随想

    8.24刘润商学院日课感悟:面试题随想 对于五道面试题当时也有过研究甚至寻找过答案。听了今天的解题思路得出其实...

  • 2.10 Python-面试题 - 子目录

    0.0 总目录 每天五道面试题(1)为什么学习Python?通过什么途径学习的Python?Python和Java...

  • 2022年,最新iOS开发笔试题-界面篇(附答案)

    前言: iOS面试题 一共分为笔试题和面试题两部分 笔试题 一共分为10个 总共613题 面试题 一共400题 笔...

  • 信息技术

    上课 准备微课(3-5分钟)(供学生随时参考) 展示学习目标 以小组为单位 评价 随堂一练(五道试题)

  • 十道前端面试题第【03】篇

    摘要:本篇分享了10道面试题——Web性能优化方案、JS严格模式、五道算法题、自定义JS事件系统、输入URL到浏览...

  • 2022年羽毛球裁判入会考试复盘

    2022.4 5.1 2022年3月羽协试题回顾 试卷一的五道题是 1.举例说明死球的情况 2.团体双打播报 3....

  • 2020—2021学年度第二学期二年级语文3/4考试试卷分析

    本次月考,有五道大题,包括基础知识、能力提升、说说心里话、阅读乐园、看图写话。虽然是月考,但是试题难易适中,能够较...

  • 每天五道面试题(8)

    json序列化时,可以处理的数据类型有哪些?如何定制支持datetime类型? json数组类型,json对象类型...

  • 每天五道面试题(4)

    xrange和range的区别? 只有在python2中才有xrange和range,python3中没有xran...

网友评论

    本文标题:笔试题五道

    本文链接:https://www.haomeiwen.com/subject/wjlpcktx.html