当前位置: 首页 > news >正文

LeetCode75——Day19

文章目录

    • 一、题目
    • 二、题解

一、题目

724. 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

二、题解

利用前缀和的思想做题

class Solution {
public:int pivotIndex(vector<int>& nums) {int n = nums.size();vector<int> prefixSum(n+1,0);for(int i = 1;i <= n;i++){prefixSum[i] = prefixSum[i-1] + nums[i-1];}for(int i = 0;i < n;i++){int left = prefixSum[i];int right = prefixSum[n] - prefixSum[i + 1];if(left == right) return i;}return -1;}
};
http://www.lryc.cn/news/210148.html

相关文章:

  • ToLua使用原生C#List和Dictionary
  • WebDAV之π-Disk派盘 + 言叶
  • Spring Security: 整体架构
  • JavaScript进阶知识汇总~
  • 理解C#中对象的浅拷贝和深拷贝
  • js 生成随机数(含随机颜色)
  • 【axios】axios的基本使用
  • React 在非组件环境切换路由
  • Oracle高速批量速插入数据解决方案
  • 基于单片机嵌入式的智能交通信号灯管理系统的设计与实现
  • 在全新ubuntu上用gpu训练paddleocr模型遇到的坑与解决办法
  • React之服务端渲染
  • jetson nano刷机更新Jetpack
  • Android官方ShapeableImageView描边/圆形/圆角图,xml布局实现
  • ubuntu扩大运行内存, 防止编译卡死
  • Kafka集群修改单个Topic数据保存周期
  • selenium模拟登录无反应
  • 指针变量未分配空间或者初始化为空指针使用问题
  • 力扣第763题 划分字母区间 c++ 哈希 + 双指针 + 小小贪心
  • js 代码中的 “use strict“; 是什么意思 ?
  • 用于读取验证码的 OCR 模型
  • Uniapp 跳转回上一页面并刷新页面数据
  • DeOldify 接口化改造 集成 Flask
  • Vue 3响应式对象: ref和reactive
  • Unity3D 如何用unity引擎然后用c#语言搭建自己的服务器
  • 带有 Vagrant 和 Virtualbox 的 Elasticsearch 集群
  • Cross Site Scripting (XSS)
  • VDA到Excel方案介绍之自定义邮件接收主题
  • 【opencv】【CPU】windows10下opencv4.8.0-cuda C++版本源码编译教程
  • 多分类loss学习记录