#include
#include
using namespace std;
class Solution {
public:
int pivotIndex(vector& nums)
{
int len = size(nums);
int sum = 0;
int left = 0;
for (int i = 0;i < len;i++)
{
sum += nums[i];
}
for (int i = 0;i < len;i++)
{
if (left * 2+nums[i] == sum)
return i;
else
left += nums[i];
}
return -1;
}
};
int main()
{
int n = 0;
cout << "please input the lenth of arr: ";
cin >> n;
vectorarr(n);
cout << "please input the elem of the arr: ";
for (int& a : arr)
cin >> a;
Solution s;
int a=s.pivotIndex(arr);
cout << a;
return 0;
}
求数组两边相等的分界线,如果没有则返回-1
最新推荐文章于 2022-04-25 17:00:11 发布