「LeetCode」剑指Offer-49丑数

142 阅读3分钟

「这是我参与2022首次更文挑战的第14天,活动详情查看:2022首次更文挑战

大家好,我是速冻鱼🐟,一条水系前端💦,喜欢花里胡哨💐,持续沙雕🌲
欢迎小伙伴们加我微信sudongyuer拉你进群
关注我的公众号:前端速冻鱼一起进步,期待与大家共同成长🥂

前言🌧️

算法,对前端人来说陌生又熟悉,很多时候我们都不会像后端工程师一样重视这项能力。但事实上,算法对每一个程序员来说,都有着不可撼动的地位。

因为开发的过程就是把实际问题转换成计算机可识别的指令,也就是《数据结构》里说的,「设计出数据结构,在施加以算法就行了」。

当然,学习也是有侧重点的,作为前端我们不需要像后端开发一样对算法全盘掌握,有些比较偏、不实用的类型和解法,只要稍做了解即可。

题目🦀

剑指 Offer 49. 丑数

难度中等

我们把只包含质因子 2、3 和 5 的数称作丑数(Ugly Number)。求按从小到大的顺序的第 n 个丑数。

示例:

输入: n = 10
输出: 12
解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

说明:

  1. 1 是丑数。
  2. n 不超过1690。

解题思路🌵

  • 此题可以通过构建最小堆来解决
  • 创建最小堆
  • 首先将1入堆
  • 弹出堆顶元素x,为当前堆中最小的堆,将2x3x5x也入堆,入堆前判断是否加入过
  • 执行n次,弹出的堆顶元素就是第n个丑数

解法🔥

/**
 * @param {number} n
 * @return {number}
 */
var nthUglyNumber = function(n) {
   const minHeap=new MinHeap()
   const set=new Set()
   const factors=[2,3,5]
    minHeap.insert(1)
    set.add(1)
    let result;
    for(let i=0;i<n;i++){
        result=minHeap.pop()
        for(const next of factors){
            if(!set.has(next*result)){
                minHeap.insert(next*result)
                set.add(next*result)
            }
        }
    }
    return result
};


 class MinHeap{

    constructor(){
        this.heap=[];
    }
    swap(indexA,indexB){
        let temp=this.heap[indexA]
        this.heap[indexA]=this.heap[indexB]
        this.heap[indexB]=temp
    }
    getParentInex(index){
        return (index-1)>>1;
    }

     getLeftChildIndex(index){
        return index*2+1;
    }
     getRightChildIndex(index){
        return index*2+2;
    }
    
    shiftUp(index){
        if(index==0){return}
        const parentIndex=this.getParentInex(index)
        //如果当前节点的父节点小于
        if(this.heap[parentIndex]>this.heap[index]){
            this.swap(parentIndex,index)
            this.shiftUp(parentIndex)
        }
    }
    shiftDown(index){
        //获取当前节点的左字节点index,进行下移
        const leftIndex=this.getLeftChildIndex(index)
        if(this.heap[index]>this.heap[leftIndex]){
            this.swap(index,leftIndex)
            this.shiftDown(leftIndex)
        }
        //获取当前节点的右边子节点index,进行下移
        const rightIndex=this.getRightChildIndex(index)
         if(this.heap[index]>this.heap[rightIndex]){
            this.swap(index,rightIndex)
            this.shiftDown(rightIndex)
        }

    }

    insert(value){
        this.heap.push(value);
        this.shiftUp(this.heap.length-1)
    }

    pop(){
        //交换父节点和末尾元素,保证堆的结构不被破坏
        if(this.heap.length===1){  return this.heap.pop();}
        const result=this.heap[0]
        this.heap[0]=this.heap.pop();
        this.shiftDown(0);
        return result
    }
    
    peek(){
        return this.heap[0];
    }

    size(){
        return this.heap.length;
    }

}

时间复杂度:O(n)

空间复杂度:O(1)

结束语🌞

那么鱼鱼的LeetCode算法篇的「LeetCode」剑指Offer-49丑数⚡️就结束了,算法这个东西没有捷径,只能多写多练,多总结,文章的目的其实很简单,就是督促自己去完成算法练习并总结和输出,菜不菜不重要,但是热爱🔥,喜欢大家能够喜欢我的短文,也希望通过文章认识更多志同道合的朋友,如果你也喜欢折腾,欢迎加我好友,一起沙雕,一起进步

github🤖:sudongyu

个人博客👨‍💻:速冻鱼blog

vx👦:sudongyuer

写在最后

伙伴们,如果喜欢我的口水话给🐟🐟点一个赞👍或者关注➕都是对我最大的支持。

加我微信:sudongyuer,邀你进群,一起学习前端,成为更优秀的工程师~(群二维码在这里->前端要午睡, 二维码过期了的话看链接沸点中的评论,我会把最新的二维码放在评论区,当然也可以加我微信我拉你进群,毕竟我也是有趣的前端,认识我也不赖🌟~