1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
724_寻找数组的中心索引_double_pointers.cpp 870 Bytes
一键复制 编辑 原始数据 按行查看 历史
/* 双指针法,不能处理存在负数的情况 */
class Solution {
public:
int pivotIndex(vector<int>& nums) {
if (nums.empty())
return -1;
int head = 0, tail = nums.size();
int sum = nums.front();
while (head < tail) {
if (sum < 0) {
head++;
sum += nums[head];
// cout << "Head : " << head << " : " << sum << endl;
}
else {
tail--;
sum -= nums[tail];
// cout << "Tail : " << tail << " : " << sum << endl;
}
}
return sum ? -1 : tail;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
git@gitee.com:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助

371d5123 14472233 46e8bd33 14472233