724 Find Pivot Index LeetCode (Google Interview Question) JavaScript

preview_player
Показать описание
724 Find Pivot Index LeetCode (Google Interview Question) JavaScript

"Given an array of integers nums, calculate the pivot index of this array.
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1."

Final code:

Gear I use:
Рекомендации по теме
Комментарии
Автор

Thanks for the explanation. Everything is clear except the part where you do -1 in the if statement. Could someone explain why this is needed? I passed the test regardless of this so I am wondering why the video does this? Kind regards

jeffreydagger
Автор

Line 10 should be 0+ 1 + 7 + 3 ... that 8 confused me for a sec

devtest
Автор

you can make a tutorial javascript for how prepare javascript for leetcode and tutorials of javascript basics for that

ProduccionesTernity
Автор

why does this need a for loop and a forEach? Can it be solved with just one forEach loop?

diamondthedev
Автор

Would this be O(1) for space and O(n) for time?

JChamberslam