cs: add “724. Find Pivot Index”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
8171cd6289
commit
7fb942c159
1 changed files with 18 additions and 0 deletions
18
cs/find-pivot-index.cs
Normal file
18
cs/find-pivot-index.cs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
public class Solution {
|
||||||
|
public int PivotIndex(int[] nums) {
|
||||||
|
var fromLeft = 0;
|
||||||
|
var fromRight = nums.Sum();
|
||||||
|
|
||||||
|
for (var i = 0; i < nums.Length; ++i) {
|
||||||
|
fromRight -= nums[i];
|
||||||
|
|
||||||
|
if (fromLeft == fromRight) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
fromLeft += nums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue