Clean code with do while statement


  • 1
    B
    public class Solution {
        public int sqrt(int x) {
            double last = x, cur = x;
            do {
                cur = last;
                last = 0.5 * (cur + x/cur);
            } while (cur-last > 0.0000005);  
            
            return (int) last;
        }
    }
    

    Not a really big deal, but I think code looks more clean with do-while


Log in to reply
 

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