leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2485.cpp (483B)


      1 class Solution {
      2   public:
      3     int pivotInteger(const int n) const {
      4         int i = 1, j = n, l = 0, h = 0;
      5         while (i < j) {
      6             if (l < h)
      7                 l += i++;
      8             else
      9                 h += j--;
     10         }
     11         return l == h ? i : -1;
     12     }
     13 };
     14 
     15 class Solution {
     16   public:
     17     int pivotInteger(const int n) const {
     18         const int sum = n * (n + 1) / 2;
     19         const int pivot = sqrt(sum);
     20         return pivot * pivot == sum ? pivot : -1;
     21     }
     22 };