题解 | #丑数#

丑数

https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param index int整型
     * @return int整型
     */
    public int GetUglyNumber_Solution(int index) {
        // write code here
        if (index == 0)
            return 0;
        List<int> listN = new List<int>() {
            1
        };
        int nIndex2 = 0, nIndex3 = 0, nIndex5 = 0;
        while (listN.Count < index) {
            int nNew2 = listN[nIndex2] * 2;
            int nNew3 = listN[nIndex3] * 3;
            int nNew5 = listN[nIndex5] * 5;
            int nNewMin = Math.Min(nNew2, Math.Min(nNew3, nNew5));
            listN.Add(nNewMin);
            if (nNewMin == nNew2) nIndex2++;
            if (nNewMin == nNew3) nIndex3++;
            if (nNewMin == nNew5) nIndex5++;
        }
        return listN[index - 1];
    }
}

全部评论

相关推荐

爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务