2 ms Java Solution - O(1)


  • 0

    Re: 256ms Java one-line solution

    Time Complexity is O(N) where N is the number of digits and which is atmax 10 (2,147,483,647). So its almost O(1)

        private int iterativeFind(int num) {
            int myNum = 0;
            while(num != 0) {
                int rem = num%10;
                num = num/10;
                
                myNum += rem;
    
                if(myNum %10 != myNum) {
                    myNum = myNum%10 + 1;
                }
            }
            
            return myNum;
        }
    

Log in to reply
 

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