首页 > 其他分享 >leetcode-724-easy

leetcode-724-easy

时间:2023-03-31 20:14:50浏览次数:31  
标签:index nums int sum 724 easy pivot leetcode left

Find Pivot Index

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.

Example 1:

Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation:
The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
Right sum = nums[4] + nums[5] = 5 + 6 = 11
Example 2:

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

Input: nums = [2,1,-1]
Output: 0
Explanation:
The pivot index is 0.
Left sum = 0 (no elements to the left of index 0)
Right sum = nums[1] + nums[2] = 1 + -1 = 0
Constraints:

1 <= nums.length <= 104
-1000 <= nums[i] <= 1000

思路一:先对整个数组求和,然后从头开始遍历数组,对比前后是否相等

    public int pivotIndex(int[] nums) {
        int sum = Arrays.stream(nums).sum();

        int sumFromBegin = 0;
        for (int i = 0; i < nums.length; i++) {
            sum -= nums[i];

            if (i > 0) {
                sumFromBegin += nums[i - 1];
            }

            if (sumFromBegin == sum) {
                return i;
            }
        }

        return -1;
    }

思路二:看了一下官解,思路更巧妙,左侧和*2+nums[i]=总和,不用求右侧和

标签:index,nums,int,sum,724,easy,pivot,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17277351.html

相关文章

  • leetcode-744-easy
    FindSmallestLetterGreaterThanTargetYouaregivenanarrayofcharacterslettersthatissortedinnon-decreasingorder,andacharactertarget.Thereareatleasttwodifferentcharactersinletters.Returnthesmallestcharacterinlettersthatis......
  • leetcode-762-easy
    PrimeNumberofSetBitsinBinaryRepresentationGiventwointegersleftandright,returnthecountofnumbersintheinclusiverange[left,right]havingaprimenumberofsetbitsintheirbinaryrepresentation.Recallthatthenumberofsetbitsan......
  • Leetcode Practice --- 栈和队列
    目录155.最小栈思路解析20.有效的括号思路解析1047.删除字符串中的所有相邻重复项思路解析1209.删除字符串中的所有相邻重复项II思路解析删除字符串中出现次数>=2次的相邻字符剑指Offer09.用两个栈实现队列239.滑动窗口最大值思路解析155.最小栈设计一个支持push......
  • LeetCode 100 相同的树
    LeetCode|100.相同的树给你两棵二叉树的根节点p和q,编写一个函数来检验这两棵树是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例1:输入:p=[1,2,3],q=[1,2,3]输出:true示例2:输入:p=[1,2],q=[1,null,2]输出:false示例3:输入:p......
  • LeetCode 94 二叉树的中序遍历
    LeetCode|94.二叉树的中序遍历给定一个二叉树的根节点root,返回它的中序 遍历。示例1:输入:root=[1,null,2,3]输出:[1,3,2]示例2:输入:root=[]输出:[]示例3:输入:root=[1]输出:[1]提示:树中节点数目在范围[0,100]内-100<=Node.val<=100迭代实现:......
  • 双网卡设备通过HIKSDK接入EasyCVR平台显示离线是什么原因?
    EasyCVR视频融合平台基于云边端协同架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台能在复杂的网络环境中,将分散的各......
  • 华为NVR设备接入EasyCVR视频融合平台后不显示摄像头的问题排查与解决
    在上期的文章中,我们和大家分享了关于EasyCVR平台与华为IVS3800平台的对接相关经验分享,感兴趣的用户可以翻阅我们往期的文章进行查看。今天我们来分享一下华为NVR设备接入平台后不显示摄像头的问题排查与解决。在EasyCVR对接华为NVR设备的过程中,通常是使用国标GB28181协议,但是有......
  • AI视频智能分析平台EasyCVR设备录像功能细节优化
    EasyCVR视频融合平台基于云边端一体化架构,具有强大的数据接入、处理及分发能力,平台支持海量视频汇聚管理,可支持多协议接入,包括市场主流标准协议与厂家私有协议及SDK,如:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、宇视SDK等(具体见下图)。平台丰富的视频能力包括:视频监控直......
  • 反转链表-leetcode92
    给你单链表的头指针head和两个整数left和right,其中left<=right。请你反转从位置left到位置right的链表节点,返回反转后的链表。示例1:输入:head=[1,2,3,4,5],left=2,right=4输出:[1,4,3,2,5]示例2:输入:head=[5],left=1,right=1输出:[5]//leet......
  • Java中使用EasyExcel生成Excel文件
    使用Spring框架中的@ExcelProperty注解生成Excel文件需要借助于第三方库,比如EasyExcel或ApachePOI等。首先定义实体类,例如publicclassUser{@ExcelProperty(value="姓名",index=0)privateStringname;@ExcelProperty(value="年龄",index=1)priva......