关于leetcode:264丑数-II-算法leetcode附思维导图-全部解法300题

4次阅读

共计 5292 个字符,预计需要花费 14 分钟才能阅读完成。

零 题目:算法(leetcode,附思维导图 + 全副解法)300 题之(264)丑数 II

一 题目形容

二 解法总览(思维导图)

三 全副解法

1 计划 1

1)代码:

// 计划 1:“本人。三指针法”。// 想法:// 因为每个数字都要被计算三次,一次是乘以 2,一次是乘以 3,一次是乘以 5,// 所以定义三个指针 —— index_2、index_3、index_5。// 这三个指针的终点是一样的,都是 0,// 如果以后的数字是乘以 2 失去的,就将 index_2 向后挪动,// 如果以后的是乘以 3 失去的,就将 index_3 向后挪动,// 如果以后的是乘以 5 失去的,就将 index_5 向后挪动。// 思路:// 1)状态初始化:resList = [1], index_2 = 0, index_3 = 0, index_5 = 0;。// 2)外围:循环解决,条件为 resList.length < n。// 2.1)求得下一个丑数 temp = Math.min(resList[index_2] * 2, resList[index_3] * 3, resList[index_5] * 5)。// 2.2)看以后丑数是乘哪个数失去的,那么对应的指针就向后挪动。// 2.3)将以后丑数塞到 数组 resList 里。// 3)返回后果 resList[n-1]。var nthUglyNumber = function(n) {// 1)状态初始化:resList = [1], index_2 = 0, index_3 = 0, index_5 = 0;。let resList = [1],
        index_2 = 0,
        index_3 = 0,
        index_5 = 0;

    // 2)外围:循环解决,条件为 resList.length < n。while(resList.length < n){// 2.1)求得下一个丑数 temp = Math.min(resList[index_2] * 2, resList[index_3] * 3, resList[index_5] * 5)。let temp = Math.min(resList[index_2] * 2, resList[index_3] * 3, resList[index_5] * 5);
        // 能够用 switch 代替 3 个 if 语句、显得逼格高。不晓得为啥行不通 很奇怪!!// switch(temp){//     case resList[index_2] * 2:
        //         index_2++;
        //         break;
        //     case resList[index_3] * 3:
        //         index_3++;
        //         break;
        //     case resList[index_5] * 5:
        //         index_5++;
        //         break;
        // }

        // 2.2)看以后丑数是乘哪个数失去的,那么对应的指针就向后挪动。if(temp === resList[index_2] * 2){index_2++;}
        if(temp === resList[index_3] * 3){index_3++;}
        if(temp === resList[index_5] * 5){index_5++;}

        // 2.3)将以后丑数塞到 数组 resList 里。resList.push(temp);
    }

    // 3)返回后果 resList[n-1]。return resList[n-1];
};

2 计划 2

1)代码:

// 计划 2“官网。最小堆法”。// 参考:// 1)https://leetcode.cn/problems/ugly-number-ii/solution/chou-shu-ii-by-leetcode-solution-uoqd/

// 思路:// 1)初始化:factors = [2, 3, 5];
// set = new Set(), heap = new MinHeap(), ugly = 1; set.add(1); heap.insert(1);。// 2)外围:循环 n - 1 次,每次都从最小堆的顶堆中取值。// 3)返回后果 ugly。// 最小堆
// TODO:从新手撕。class MinHeap {constructor() {this.heap = [];
    }

    getParentIndex(i) {return (i - 1) >> 1;
    }

    getLeftIndex(i) {return i * 2 + 1;}

    getRightIndex(i) {return i * 2 + 2;}

    shiftUp(index) {if(index === 0) {return;}

        const parentIndex = this.getParentIndex(index);
        if(this.heap[parentIndex] > this.heap[index]){this.swap(parentIndex, index);
            this.shiftUp(parentIndex);
        }  
    }

    swap(i1, i2) {const temp = this.heap[i1];
        this.heap[i1]= this.heap[i2];
        this.heap[i2] = temp;
    }

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

    pop() {this.heap[0] = this.heap.pop();
        this.shiftDown(0);
        return this.heap[0];
    }

    shiftDown(index) {const leftIndex = this.getLeftIndex(index);
        const rightIndex = this.getRightIndex(index);

        if (this.heap[leftIndex] < this.heap[index]) {this.swap(leftIndex, index);
            this.shiftDown(leftIndex);
        }
        if (this.heap[rightIndex] < this.heap[index]){this.swap(rightIndex, index);
            this.shiftDown(rightIndex);
        }
    }

    peek() {return this.heap[0];
    }

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

var nthUglyNumber = function(n) {// 1)初始化:factors = [2, 3, 5];
    // set = new Set(), heap = new MinHeap(), ugly = 1; set.add(1); heap.insert(1);。const factors = [2, 3, 5];
    let set = new Set(),
        heap = new MinHeap(),
        ugly = 1;
    set.add(1);
    heap.insert(1);

    // 2)外围:循环 n - 1 次,每次都从最小堆的顶堆中取值。for (let i = 0; i < n; i++) {ugly = heap.pop();
        for (const factor of factors) {
            const next = ugly * factor;
            if (!set.has(next)) {set.add(next);
                heap.insert(next);
            }
        }
        
    }

    // 3)返回后果 ugly。return ugly;
};

四 资源分享 & 更多

1 历史文章 – 总览

文章名称 解法 浏览量
1. 两数之和(Two Sum) 共 3 种 2.7 k+
2. 两数相加(Add Two Numbers) 共 4 种 2.7 k+
3. 无反复字符的最长子串(Longest Substring Without Repeating Characters) 共 3 种 2.6 k+
4. 寻找两个正序数组的中位数(Median of Two Sorted Arrays) 共 3 种 2.8 k+
5. 最长回文子串(Longest Palindromic Substring) 共 4 种 2.8 k+
6. Z 字形变换(ZigZag Conversion) 共 2 种 1.9 k+
7. 整数反转(Reverse Integer) 共 2 种 2.4 k+
8. 字符串转换整数 (atoi)(String to Integer (atoi)) 共 3 种 4.2 k+
9. 回文数(Palindrome Number) 共 3 种 4.3 k+
11. 盛最多水的容器(Container With Most Water) 共 5 种 4.0 k+
12. 整数转罗马数字(Integer to Roman) 共 3 种 3.2 k+
13. 罗马数字转整数(Roman to Integer) 共 3 种 3.8 k+
14. 最长公共前缀(Longest Common Prefix) 共 4 种 3.0 k+
15. 三数之和(3Sum) 共 3 种 60.7 k+
16. 最靠近的三数之和(3Sum Closest) 共 3 种 4.7 k+
17. 电话号码的字母组合(Letter Combinations of a Phone Number) 共 3 种 3.1 k+
18. 四数之和(4Sum) 共 4 种 11.5 k+
19. 删除链表的倒数第 N 个结点(Remove Nth Node From End of List) 共 4 种 1.2 k+
20. 无效的括号(Valid Parentheses) 共 2 种 1.8 k+
21. 合并两个有序链表(Merge Two Sorted Lists) 共 3 种 1.2 k+
22. 括号生成(Generate Parentheses) 共 4 种 1.1 k+
23. 合并 K 个升序链表(Merge k Sorted Lists) 共 4 种 0.9 k+
24. 两两替换链表中的节点(Swap Nodes in Pairs) 共 3 种 0.5 k+
25. K 个一组翻转链表(Reverse Nodes in k-Group) 共 5 种 1.3 k+
26. 删除有序数组中的反复项(Remove Duplicates from Sorted Array) 共 4 种 1.3 k+
27. 移除元素(Remove Element) 共 4 种 0.4 k+
28. 实现 strStr()(Implement strStr()) 共 5 种 0.8 k+
29. 两数相除(Divide Two Integers) 共 4 种 0.6 k+
30. 串联所有单词的子串(Substring with Concatenation of All Words) 共 3 种 0.6 k+
31. 下一个排列(Next Permutation) 共 2 种 0.8 k+
32. 最长无效括号(Longest Valid Parentheses) 共 2 种 1.4 k+
33. 搜寻旋转排序数组(Search in Rotated Sorted Array) 共 3 种 1.0k+
34. 在排序数组中查找元素的第一个和最初一个地位(Find First and Last Position of Element in Sorted Array) 共 3 种 0.5 k+
35. 搜寻插入地位(Search Insert Position) 共 3 种 0.3 k+
36. 无效的数独(Valid Sudoku) 共 1 种 0.6 k+
38. 外观数列(Count and Say) 共 5 种 1.1 k+
39. 组合总和(Combination Sum) 共 3 种 1.4 k+
40. 组合总和 II(Combination Sum II) 共 2 种 1.6 k+
41. 缺失的第一个负数(First Missing Positive) 共 3 种 1.2 k+
53. 最大子数组和(Maximum Subarray) 共 3 种 0.3k+
88. 合并两个有序数组(Merge Sorted Array) 共 3 种 0.4 k+
102. 二叉树的层序遍历(Binary Tree Level Order Traversal) 共 3 种 0.4 k+
146. LRU 缓存(LRU Cache) 共 2 种 0.5 k+
160. 相交链表(Intersection of Two Linked Lists) 共 2 种 0.1 k+
200. 岛屿数量(Number of Islands) 共 4 种 0.1 k+
206. 反转链表(Reverse Linked List) 共 3 种 1.0 k+
215. 数组中的第 K 个最大元素(Kth Largest Element in an Array) 共 3 种 0.5 k+
236. 二叉树的最近公共先人(Lowest Common Ancestor of a Binary Tree) 共 3 种 0.1 k+
2119. 反转两次的数字(A Number After a Double Reversal) 共 2 种 0.3 k+
2120. 执行所有后缀指令(Execution of All Suffix Instructions Staying in a Grid) 共 1 种 0.4 k+
2124. 查看是否所有 A 都在 B 之前(Check if All A’s Appears Before All B’s) 共 4 种 0.4 k+
2125. 银行中的激光束数量(Number of Laser Beams in a Bank) 共 3 种 0.3 k+
2126. 捣毁小行星(Destroying Asteroids) 共 2 种 1.6 k+
2129. 将题目首字母大写(Capitalize the Title) 共 2 种 0.6 k+
2130. 链表最大孪生和(Maximum Twin Sum of a Linked List) 共 2 种 0.6 k+
2133. 查看是否每一行每一列都蕴含全副整数(Check if Every Row and Column Contains All Numbers) 共 1 种 0.6 k+

2 博主简介

码农三少,一个致力于编写 极简、但齐全题解(算法 )的博主。
专一于 一题多解、结构化思维,欢送一起刷穿 LeetCode ~

正文完
 0