寻找数组的中心索引
This commit is contained in:
parent
bb8f35cfef
commit
28542a33ef
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
target
|
||||
.idea
|
||||
|
||||
*.swp
|
||||
|
39
src/list/PivotIndex.java
Normal file
39
src/list/PivotIndex.java
Normal file
@ -0,0 +1,39 @@
|
||||
package list;
|
||||
|
||||
|
||||
/**
|
||||
* @file PivotIndex.java
|
||||
* @apiNote https://leetcode-cn.com/leetbook/read/array-and-string/yf47s/
|
||||
* @author zeekling
|
||||
* @version 1.0
|
||||
* @date 2020-08-16
|
||||
*/
|
||||
public class PivotIndex {
|
||||
|
||||
|
||||
public int pivotIndex(int[] nums){
|
||||
int sum = 0;
|
||||
int len = nums.length;
|
||||
for (int i=0; i< len;i++){
|
||||
sum += nums[i];
|
||||
}
|
||||
int idx = -1;
|
||||
int leftSum = 0;
|
||||
for (int i=0;i<len;i++){
|
||||
|
||||
if ((leftSum * 2 + nums[i]) == sum){
|
||||
return i;
|
||||
}
|
||||
leftSum += nums[i];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
int[] arr = new int[]{1, 7, 3, 6, 5, 6};
|
||||
PivotIndex pro = new PivotIndex();
|
||||
int res = pro.pivotIndex(arr);
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user