Two-Line C++ Solution Using Sum of Consecutive Odd Numbers


  • 0

    Similar to other solutions, just condensed to two lines for those codebatters out there.

    class Solution {
    public:
        bool isPerfectSquare(int num) {
            for (int oddNumber = 1; num > 0; num -= oddNumber, oddNumber += 2);
            return !num;
        }
    };
    

Log in to reply
 

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