C# straight forward solution with three indcator


  • 0
    Y
    public class Solution {
        public int NthUglyNumber(int n) {
            int[] ugArray=new int[n];
            ugArray[0]=1;
            int twIn=0,thrIn=0,finIn=0,curIn=0;
            while(curIn<n-1)
            {
                int two=2*ugArray[twIn];
                int thr=3*ugArray[thrIn];
                int fiv=5*ugArray[finIn];
                if(two<=thr&&two<=fiv)
                {
                    if(two!=ugArray[curIn])
                    {
                        curIn++;
                        ugArray[curIn]=two;
                    }
                    twIn++;
                }
                else if(thr<=two&&thr<=fiv)
                {
                    if(thr!=ugArray[curIn])
                    {
                        curIn++;
                        ugArray[curIn]=thr;
                    }
                    thrIn++; 
                }
                else
                {
                    if(fiv!=ugArray[curIn])
                    {
                        curIn++;
                        ugArray[curIn]=fiv;
                    }
                    finIn++; 
                }
            }
            return ugArray[n-1];
        }
        
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.