首页 > 其他分享 >【数组】LeetCode 264. 丑数 II

【数组】LeetCode 264. 丑数 II

时间:2023-03-07 15:36:12浏览次数:41  
标签:丑数 min int currentNumber II uglyNumbers weightIndex LeetCode

题目链接

264. 丑数 II

思路

根据题目中的样例,可以进行拆分

\[1, 1×2, 1×3, 2×2, 1×5, 2×3, 2×4, 3×3, 3×4, 3×5 \]

观察能发现,这些多项式能分成下面三组:

\[乘 2: 1×2, 2×2, 3×2, 4×2, 5×2, 6×2, 8×2, 9×2,… \\ 乘 3: 1×3, 2×3, 3×3, 4×3, 5×3, 6×3, 8×3, 9×3,… \\ 乘 5: 1×5, 2×5, 3×5, 4×5, 5×5, 6×5, 8×5, 9×5,… \\ \]

可以发现新的丑数都是利用之前的丑数乘以2、3或者5计算得到的。

我们可以使用三个指针,分别指向乘以2、乘以3、乘以5的丑数的位置,每次取最小值再放入序列中,同时更新乘积与最小值相等的丑数索引,计算n次后便得到第n个丑数

代码

class Solution {
    public int nthUglyNumber(int n) {
        int[] weightIndex = new int[]{0, 0, 0};
        int[] base = new int[]{2, 3, 5};
        int[] currentNumber = new int[3];
        ArrayList<Integer> uglyNumbers = new ArrayList<>();
        uglyNumbers.add(1);

        // we already have "1" in vector, we just need to do n-1 times
        for(int i = 1; i < n; i++){
            int min = uglyNumbers.get(weightIndex[0]) * base[0];
            for(int j = 0; j < 3; j++){
                currentNumber[j] = uglyNumbers.get(weightIndex[j]) * base[j];
                min = Math.min(min, currentNumber[j]);
            }
            uglyNumbers.add(min);

            if(min == currentNumber[0]){
                weightIndex[0]++;
            }
            if(min == currentNumber[1]){
                weightIndex[1]++;
            }
            if(min == currentNumber[2]){
                weightIndex[2]++;
            }
        }

        return uglyNumbers.get(n - 1);
    }
}

标签:丑数,min,int,currentNumber,II,uglyNumbers,weightIndex,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17188231.html

相关文章

  • 数组-leetcode-485
    ​​0️⃣python数据结构与算法学习路线​​学习内容:基本算法:枚举、排序、搜索、递归、分治、优先搜索、贪心、双指针、动态规划等…数据结构:字符串(string)、列表(list)、元......
  • 数组-Leetcode-697
    ​​0️⃣python数据结构与算法学习路线​​学习内容:基本算法:枚举、排序、搜索、递归、分治、优先搜索、贪心、双指针、动态规划等…数据结构:字符串(string)、列表(list)、元......
  • LeetCode题分类
    一.数组题目分类题目编号数组的遍历485、495、414、628统计数组中的元素645、697、448、442、41、274数组的改变、移动453、665、283二维数组及滚动数组118、119......
  • 动态规划-leetcode-494
    ​​0️⃣python数据结构与算法学习路线​​学习内容:基本算法:枚举、排序、搜索、递归、分治、优先搜索、贪心、双指针、动态规划等…数据结构:字符串(string)、列表(list)、元......
  • 【队列】LeetCode 1429. 第一个唯一数字
    题目链接1429.第一个唯一数字给定一系列整数,插入一个队列中,找出队列中第一个唯一整数。实现FirstUnique类:FirstUnique(int[]nums)用数组里的数字初始化队列。in......
  • 【二分查找】LeetCode 4. 寻找两个正序数组的中位数
    题目链接4.寻找两个正序数组的中位数思路分治代码classSolution{publicdoublefindMedianSortedArrays(int[]nums1,int[]nums2){if(nums1.len......
  • 80. Remove Duplicates from Sorted Array II
    ##题目Followupfor“RemoveDuplicates”:Whatifduplicatesareallowedatmosttwice?Forexample,Givensortedarraynums=[1,1,1,2,2,3],Yourfunction......
  • 81. Search in Rotated Sorted Array II
    ##题目Followupfor“SearchinRotatedSortedArray”:Whatifduplicatesareallowed?Wouldthisaffecttherun-timecomplexity?Howandwhy?Supposeana......
  • 82. Remove Duplicates from Sorted List II
    ##题目Givenasortedlinkedlist,deleteallnodesthathaveduplicatenumbers,leavingonlydistinctnumbersfromtheoriginallist.Forexample,Given1->......
  • 731. My Calendar II
    题目ImplementaMyCalendarTwoclasstostoreyourevents.Aneweventcanbeaddedifaddingtheeventwillnotcauseatriplebooking.Yourclasswil......