【leetcode】264. 丑数 II

 

#define min(a,b) ((a)<(b))?(a):(b)
int nthUglyNumber(int n){
    int nums[1691] = {0};
    nums[0]=1;
    int ugly, i2 = 0, i3 = 0, i5 = 0;
    for(int i = 1; i < n; ++i) {
      ugly = min( min(nums[i2] * 2, nums[i3] * 3), nums[i5] * 5);
      nums[i] = ugly;

      if (ugly == nums[i2] * 2) ++i2;
      if (ugly == nums[i3] * 3) ++i3;
      if (ugly == nums[i5] * 5) ++i5;
    }

    return nums[n-1];
}

 

posted @ 2021-01-07 19:21  温暖了寂寞  阅读(64)  评论(0编辑  收藏  举报