头图

Title Zero: Algorithm (leetcode, with mind map + all solutions) of 300 questions (264) Ugly Number II

a topic description

题目描述

Two solutions overview (mind map)

思维导图

All three solutions

1 Scenario 1

1) Code:

 // 方案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 Option 2

1) Code:

 // 方案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;
};

Four resource sharing & more

1 Historical Articles - Overview

Article name solution reading volume
1. Two Sum 3 types in total 2.7k+
2. Add Two Numbers 4 types in total 2.7k+
3. Longest Substring Without Repeating Characters 3 types in total 2.6k+
4. Find the Median of Two Sorted Arrays 3 types in total 2.8k+
5. Longest Palindromic Substring 4 types in total 2.8k+
6. ZigZag Conversion 2 types in total 1.9k+
7. Reverse Integer 2 types in total 2.4k+
8. String to Integer (atoi) 3 types in total 4.2k+
9. Palindrome Number 3 types in total 4.3k+
11. Container With Most Water 5 in total 4.0k+
12. Integer to Roman 3 types in total 3.2k+
13. Roman to Integer 3 types in total 3.8k+
14. Longest Common Prefix 4 types in total 3.0k+
15. The Sum of Three Numbers (3Sum) 3 types in total 60.7k+
16. 3Sum Closest 3 types in total 4.7k+
17. Letter Combinations of a Phone Number 3 types in total 3.1k+
18. The sum of four numbers (4Sum) 4 types in total 11.5k+
19. Remove Nth Node From End of List 4 types in total 1.2k+
20. Valid Parentheses 2 types in total 1.8k+
21. Merge Two Sorted Lists 3 types in total 1.2k+
22. Generate Parentheses 4 types in total 1.1k+
23. Merge k Sorted Lists 4 types in total 0.9k+
24. Swap Nodes in Pairs 3 types in total 0.5k+
25. Reverse Nodes in k-Group 5 in total 1.3k+
26. Remove Duplicates from Sorted Array 4 types in total 1.3k+
27. Remove Element 4 types in total 0.4k+
28. Implement strStr() (Implement strStr()) 5 in total 0.8k+
29. Divide Two Integers 4 types in total 0.6k+
30. Substring with Concatenation of All Words 3 types in total 0.6k+
31. Next Permutation 2 types in total 0.8k+
32. Longest Valid Parentheses 2 types in total 1.4k+
33. Search in Rotated Sorted Array 3 types in total 1.0k+
34. Find First and Last Position of Element in Sorted Array 3 types in total 0.5k+
35. Search Insert Position 3 types in total 0.3k+
36. Valid Sudoku 1 in total 0.6k+
38. Count and Say 5 in total 1.1k+
39. Combination Sum 3 types in total 1.4k+
40. Combination Sum II 2 types in total 1.6k+
41. First Missing Positive 3 types in total 1.2k+
53. Maximum Subarray Sum (Maximum Subarray) 3 types in total 0.3k+
88. Merge Sorted Array 3 types in total 0.4k+
102. Binary Tree Level Order Traversal 3 types in total 0.4k+
146. LRU Cache (LRU Cache) 2 types in total 0.5k+
160. Intersection of Two Linked Lists 2 types in total 0.1k+
200. Number of Islands 4 types in total 0.1k+
206. Reverse Linked List 3 types in total 1.0k+
215. Kth Largest Element in an Array 3 types in total 0.5k+
236. Lowest Common Ancestor of a Binary Tree 3 types in total 0.1k+
2119. A Number After a Double Reversal 2 types in total 0.3k+
2120. Execution of All Suffix Instructions Staying in a Grid 1 in total 0.4k+
2124. Check if All A's Appears Before All B's 4 types in total 0.4k+
2125. Number of Laser Beams in a Bank 3 types in total 0.3k+
2126. Destroying Asteroids 2 types in total 1.6k+
2129. Capitalize the Title 2 types in total 0.6k+
2130. Maximum Twin Sum of a Linked List 2 types in total 0.6k+
2133. Check if Every Row and Column Contains All Numbers 1 in total 0.6k+

刷题进度 - LeetCode:561 / 2695 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝