Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6ea024

Browse files
committed
724_Find_Pivot_Index
1 parent d462877 commit b6ea024

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Also, there are open source implementations for basic data structs and algorithm
160160
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
161161
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
162162
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |
163+
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/724_Find_Pivot_Index.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/724_Find_Pivot_Index.java) | Seach the array to find a place where left sum is equal to right sum, O(n) and O(1) |
163164
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|
164165
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
165166
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |

java/724_Find_Pivot_Index.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int pivotIndex(int[] nums) {
3+
int totalsum = 0, leftsum = 0;
4+
// Compute total sum
5+
for (int i = 0; i < nums.length; i++) totalsum += nums[i];
6+
// Check leftsum == rightsum
7+
for (int i = 0; i < nums.length; i++) {
8+
if (leftsum == totalsum - leftsum - nums[i]) return i;
9+
leftsum += nums[i];
10+
}
11+
return -1;
12+
}
13+
}

python/724_Find_Pivot_Index.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def pivotIndex(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
totalsum = sum(nums)
8+
leftsum = 0
9+
for i, v in enumerate(nums):
10+
# leftsum == rightsum
11+
if leftsum == totalsum - leftsum - v:
12+
return i
13+
leftsum += v
14+
return -1

0 commit comments

Comments
 (0)