zoukankan      html  css  js  c++  java
  • [LeetCode] Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this array.

    We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

    If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

    Example 1:

    Input: 
    nums = [1, 7, 3, 6, 5, 6]
    Output: 3
    Explanation: 
    The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
    Also, 3 is the first index where this occurs.
    

    Example 2:

    Input: 
    nums = [1, 2, 3]
    Output: -1
    Explanation: 
    There is no index that satisfies the conditions in the problem statement.
    

    Note:

    • The length of nums will be in the range [0, 10000].
    • Each element nums[i] will be an integer in the range [-1000, 1000].

    A brute force solution is for each index, calculate the sum on its left and right side. This gives a O(n^2) runtime.

    A better solution is to use prefix sum to avoid repeatedly computing the sum of the same subarray. 

    Solution 1. O(n) runtime, O(n) space 

     1 class Solution {
     2     public int pivotIndex(int[] nums) {
     3         if(nums == null || nums.length == 0) {
     4             return -1;
     5         }
     6         int n = nums.length;
     7         int[] prefixSum = new int[n];
     8         prefixSum[0] = nums[0];
     9         for(int i = 1; i < n; i++) {
    10             prefixSum[i] = prefixSum[i - 1] + nums[i];
    11         }
    12         int leftSum, rightSum, pivotIndex;
    13         for(pivotIndex = 0; pivotIndex < n; pivotIndex++) {
    14             if(pivotIndex == 0) {
    15                 leftSum = 0;
    16             }
    17             else {
    18                 leftSum = prefixSum[pivotIndex - 1];
    19             }
    20             rightSum = prefixSum[n - 1] - prefixSum[pivotIndex];
    21             if(leftSum == rightSum) {
    22                 return pivotIndex;
    23             }
    24         }
    25         return -1;
    26     }
    27 }

    Even better, we can solve this problem using only O(1) space without actually storing the entire prefix result. A total sum of the entire input array is all we need.

     1 class Solution {
     2     public int pivotIndex(int[] nums) {
     3         int sum = 0, left = 0;
     4         for (int i = 0; i < nums.length; i++) sum += nums[i];
     5         
     6         for (int i = 0; i < nums.length; i++) {
     7             if (i != 0) left += nums[i - 1];
     8             if (sum - left - nums[i] == left) return i;
     9         }
    10         
    11         return -1;
    12     }
    13 }
  • 相关阅读:
    java基础篇3---变量值转换(不使用中间变量)
    java基础篇2---逆序输出
    Java基础篇1---数据类型转换
    java基础篇8-----字符大小写字母的转换
    java基础篇7----java.util中的Scanner类的使用
    java基础篇0----运算符
    java基础篇5---循环结构
    ECMAScript arguments 对象(摘自W3C)
    JS中的this的应用总结
    定时器 + 简单的动画效果
  • 原文地址:https://www.cnblogs.com/lz87/p/9919790.html
Copyright © 2011-2022 走看看