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)


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