zoukankan      html  css  js  c++  java
  • 33、求按从小到大的顺序的第N个丑数

    一、题目

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

    二、解法

     1 public class Solution {
     2    public int GetUglyNumber_Solution(int index) {
     3          if(index <= 0)
     4              return 0;
     5          if(index < 7)
     6              return index;
     7          int[] res = new int[index];
     8          res[0] = 1;
     9          int t2 = 0,//记录乘以2的个数
    10              t3 = 0,//记录乘以3的个数
    11              t5 = 0,//记录乘以5的个数
    12              i  = 1;
    13          for(; i < index; i++){
    14              res[i] = Math.min(res[t2]*2, Math.min(res[t3]*3, res[t5]*5));
    15              if(res[i] == res[t2]*2) t2++;//如果最小的等于res[t2]*2,那么t2加1
    16              if(res[i] == res[t3]*3) t3++;
    17              if(res[i] == res[t5]*5) t5++;
    18          }
    19          return res[index-1];
    20         }
    21 }
  • 相关阅读:
    pwd命令
    python-windows环境安装
    python介绍
    elk安装
    elk介绍
    111
    使用CEF作为用户界面
    使用CEF作为浏览器
    c# 内嵌chrome(Webkit)
    待搞清楚
  • 原文地址:https://www.cnblogs.com/fankongkong/p/7455517.html
Copyright © 2011-2022 走看看