• 剑指offer(33)丑数


    题目描述

    把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

    题目分析

    主要在于理解丑数的概念,只包含因子2、3和5的数称作丑数,那么我们可以先把因子2、3和5分离出来,那么剩下的就是其他因子,看是否为1,为1的话说明没有其他因子,那就为丑数。不是1的话说明有其他因子,那么就不是丑数。

    第一种暴力解法,缺点是连非丑数的也计算力,会超时。

    第二种用到了动态规划的思想,把前面的丑数存着,生成后面的丑数。t2,t3,t5是判断点,用于判断从何处开始选出并乘以对应因子肯定会大于当前数组中最大丑数,而前面的丑数不用考虑。

    代码

    第一种(超时)

    function GetUglyNumber_Solution(index) {
      if (index <= 1) return 0;
      let count = 0;
      let num = 0;
      while (count < index) {
        num++;
        if (isUgly(num)) {
          count++;
        }
      }
      return num;
    }
    function isUgly(num) {
      while (num % 2 === 0) num /= 2;
      while (num % 3 === 0) num /= 3;
      while (num % 5 === 0) num /= 5;
      return num === 1;
    }

     第二种:(以空间换时间)

    function GetUglyNumberSolution(index) {
      if (index < 7) return index;
      const res = [];
      res[0] = 1;
      let t2 = 0,
        t3 = 0,
        t5 = 0;
      for (let i = 1; i < index; i++) {
        res[i] = Math.min(res[t2] * 2, ress[t3] * 3, res[t5] * 5);
        if (res[i] === res[t2] * 2) t2++;
        if (res[i] === res[t3] * 3) t3++;
        if (res[i] === res[t5] * 5) t5++;
      }
      return res[index - 1];
    }
  • 相关阅读:
    tsql导出EXCEL语句待测试
    IE和Firefox下event乱谈
    cf 167.d( 多重集全排列 )
    poj 1815(最小割 + 枚举)
    Codeforces Round #167 (Div. 2)
    poj 3308(最小割+对数处理)
    在FootTemplate与ItemTemplate中间扩展Repeater模板
    在没有高度设定的情况下让margintop生效
    float:left后整体居中
    判断ExecuteScalar()是否返回结果
  • 原文地址:https://www.cnblogs.com/wuguanglin/p/GetUglyNumber_Solution.html
Copyright © 2020-2023  润新知