给定一个整数类型的数组 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. ChatGPT如何助力DevOps|用例解读

    DevOps 是一种方法论,旨在提高软件开发和 IT 运营团队的协作和效率.DevOps 涉及各种任务和流程的自动化,例如规划.编码.测试.部署.监控和故障排除.然而,其中一些任务和流程仍然有大量任务 ...

  2. CTF-NEFU校赛-题解

    Write by NEFUNSI: ghosin 0ERROR 签到 signin 下载 signin.txt 打开得到一串 base64,解码得到 flag{we1come_t0_NEFUCTF!} ...

  3. 随机服务系统模拟—R实现(三)

    M/M/c随机服务系统的模拟 M/M/1服务系统:(1)队列长度没有限制:(2)顾客到达的时间间隔和服务时间均服从指数分布:(3)服务台数量为c. 一.M/M/c随机服务系统的模拟 在M/M/c排队系 ...

  4. 在k8s安装CICD-devtron

    在k8s安装CICD-devtron 先前条件 <kubernetes(k8s) 存储动态挂载>参考我之前的文档进行部署https://www.oiox.cn/index.php/arch ...

  5. IBM Cloud Computing Practitioners 2019 (IBM云计算从业者2019)Exam答案

    Cloud Computing Practitioners 2019 IBM Cloud Computing Practitioners 2019 (IBM云计算从业者2019)Exam答案,加粗的为 ...

  6. [Python]PyCharm中出现unresolved reference的解决方法

    1 问题描述 2 解决方法 将你的目录添加sources root,即可解决此问题 (工程目录)>右键>Mark Directory As > Sources Root X 参考文献 ...

  7. 5.mapper出错原因

    1.总结:前个星期mapper出错,很大原因是自己的项目结构创建有问题,大项目下应该是spring init那种项目结构形式,但是在创建多模块的时候应该是使用moudle形式的项目结构: 所以自己在运 ...

  8. DES算法图解、密码学

  9. javasec(四)序列化与反序列化基本原理

    title: javasec(四)序列化与反序列化基本原理 tags: - javasec - 反序列化 categories: - javasec cover: 'https://blog-1313 ...

  10. 这可能是最全面的MySQL面试八股文了

    什么是MySQL MySQL是一个关系型数据库,它采用表的形式来存储数据.你可以理解成是Excel表格,既然是表的形式存储数据,就有表结构(行和列).行代表每一行数据,列代表该行中的每个值.列上的值是 ...