article thumbnail image
Published 2022. 8. 26. 10:05
 

Find Pivot Index - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

int pivotIndex(vector<int>& nums){
	int left_sum = 0;
	int right_sum = std::accumulate(nums.begin(), nums.end(), 0);
	int current_left = 0;

	for(int i = 0; i < nums.size(); i++){
		left_sum += current_left;
		right_sum -= nums[i];
		if(left_sum == right_sum){
			return i;
		}
		current_left = nums[i];
	}
	return -1;
}

'Problem set' 카테고리의 다른 글

[LeetCode] Sort Colors  (0) 2022.08.30
[LeetCode] Sort-Array-By-Parity-ii  (0) 2022.08.26
[LeetCode] Move Zeroes  (0) 2022.08.26
[백준] 1967 트리의 지름  (0) 2021.02.20
[백준] 1167 트리의 지름  (0) 2021.02.20
복사했습니다!