丑数——剑指offer面试题34

int GetUglyNumber(int index)
{
    if(index<=0)
        return 0;
    int *pUglyNumbers=new int[index];
    pUglyNumbers[0]=1;
    int nextUglyIndex=1;

    int *pMultiply2=pUglyNumbers;
    int *pMultiply3=pUglyNumbers;
    int *pMultiply5=pUglyNumbers;

    while(nextUglyIndex<index)
    {
        int min=min(*pMultiply2*2,*pMultiply3*3,*pMultiply5*5);
        pUglyNumbers[nextUglyIndex]=min;

        while(*pMultiply2*2<=pUglyNumbers[nextUglyIndex])
            ++pMultiply2;
        while(*pMultiply3*3<=pUglyNumbers[nextUglyIndex])
            ++pMultiply3;
        while(*pMultiply5*5<=pUglyNumbers[nextUglyIndex])
            ++pMultiply5;

        ++nextUglyIndex;
    }
}

int min(int number1,int number2,int number3)
{
    int min=(number1<number2)?number1:number2;
    min=(min<number3)?min:number3;
    return min;
}

 

posted @ 2015-08-11 20:20  wy1290939507  阅读(116)  评论(0编辑  收藏  举报