给定一个整数类型的数组 nums,请编写一个能够返回数组 “中心索引” 的方法。

我们是这样定义数组 中心索引 的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。

如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。

 

示例 1:

输入:
nums = [1, 7, 3, 6, 5, 6]
输出:3
解释:
索引 3 (nums[3] = 6) 的左侧数之和 (1 + 7 + 3 = 11),与右侧数之和 (5 + 6 = 11) 相等。
同时, 3 也是第一个符合要求的中心索引。

示例 2:

输入:
nums = [1, 2, 3]
输出:-1
解释:
数组中不存在满足此条件的中心索引。

说明:

    nums 的长度范围为 [0, 10000]。
    任何一个 nums[i] 将会是一个范围在 [-1000, 1000]的整数。

Rust Solution:

 1 fn pivot_index(nums: Vec<i32>) -> i32 {
2 // S 是数组的和,当索引 i 是中心索引时,位于 i 左边数组元素的和 leftsum 满足 S - nums[i] - leftsum。
3 // 我们只需要判断当前索引 i 是否满足 leftsum==S-nums[i]-leftsum 并动态计算 leftsum 的值
4
5 let sum = nums.iter().sum::<i32>();
6 let len = nums.len();
7 if len <= 2 {
8 return -1;
9 }
10
11 for i in 0..len {
12 let lh_sum: i32= nums[..i].iter().sum();
13 println!("lh_sum = {}", lh_sum);
14 if lh_sum == sum - lh_sum - nums[i] {
15 println!("rh sum = {}", sum - lh_sum - nums[i]);
16 return i as i32;
17 }
18 }
19 -1 // if nums is empty return
20 }

C++ Solution:

 1 struct Sum {
2 void operator()(int n) {
3 sum += n;
4 }
5
6 Sum(): sum(0){}
7
8 int sum;
9 };
10
11 int pivotIndex(vector<int>& nums) {
12 Sum s = for_each(nums.begin(), nums.end(), Sum());
13 int left_sum = 0;
14 for(auto i = 0; i < nums.size(); i++){
15 if (left_sum == s.sum - nums[i] - left_sum) {
16 return i;
17 }
18 left_sum += nums[i];
19 }
20 return -1;
21 }

Python Solution:

1 def pivot_index(nums: List[int]) -> int:
2 s = sum(nums)
3 left_sum = 0
4 for i, x in enumerate(nums):
5 if left_sum == (s - left_sum - x):
6 return i
7 left_sum += x
8 return -1

Github Solution Link :https://github.com/DaviRain-Su/leetcode_solution/tree/master/find_array_center_index

【LeetCode】Find Pivot Index #724 Rust Solution的更多相关文章

  1. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...

  2. 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  6. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  7. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  10. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

随机推荐

  1. mixins使用混入引入组件,并可以使用公共函数。组件类同名函数可以替代公共函数。使用$ref获得子元素数据和元素dom节点。使用$parents获得父元素数据。slot插槽的使用

    父组件: <template> <div class="box"> <Header > <div slot="left" ...

  2. img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案

      问题描述: 在使用img2pdf转换png到pdf时候,报了如下错误 img2pdf.AlphaChannelError: Refusing to work on images with alph ...

  3. 构建基于深度学习神经网络协同过滤模型(NCF)的视频推荐系统(Python3.10/Tensorflow2.11)

    毋庸讳言,和传统架构(BS开发/CS开发)相比,人工智能技术确实有一定的基础门槛,它注定不是大众化,普适化的东西.但也不能否认,人工智能技术也具备像传统架构一样"套路化"的流程,也 ...

  4. 移动端网页--better-scroll容易采坑合集

    移动端网页--better-scroll容易采坑合集 一.better-scroll源码bug,浏览器需要刷新一次才能正常滑动 在new BScroll时,在options中加入 mouseWheel ...

  5. 补五月四号java基础知识

    1.在JDK5中新增了自动包装和自动解包功能:当编译器发现程序再应该使用包装类对象的地方却使用基本数据类型的数据时,编译器将自动把该数据包装为该基本数据对应的包装类的对象,这个过程成为自动包装.如类型 ...

  6. Nvidia GPU热迁移-Singularity

    1 背景 在GPU虚拟化和池化的加持下,可以显著提高集群的GPU利用率,同时也可以较好地实现弹性伸缩.但有时会遇到需要GPU资源再分配的场景,此时亟需集群拥有GPU任务热迁移的能力.举个简单的例子,比 ...

  7. 亿级Web系统负载均衡几种实现方式

    负载均衡(Load Balance)是集群技术(Cluster)的一种应用技术.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用是Web负载均衡.根据实现的原理 ...

  8. 【从零开始】Docker Desktop:听说你小子要玩我

    前言 缘由 捡起遗忘的Docker知识 由于本狗近期项目紧任务重,高强度的搬砖导致摸鱼时间下降.在上线项目时,看到运维大神一系列骚操作,docker+k8s的知识如过眼云烟,忘得干净的很.所以想重新恶 ...

  9. 深度学习-09(目标检测:Object Detection)

    文章目录 目标检测(Object Detection) 一 .基本概念 1. 什么是目标检测 2. 目标检测的核心问题 3. 目标检测算法分类 4. 目标检测应用 目标检测原理 1.候选区域产生 1 ...

  10. abp(net core)+easyui+efcore实现仓储管理系统——供应商管理升级之下(六十四)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统--ABP总体介绍(一) abp(net core)+ ...