给定一个整数类型的数组 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-4模型

    OpenAI免费版基于Gpt3.5,无法使用最新发布的 GPT-4 模型,必须开通 ChatGPT Plus.但是 OpenAI 不但屏蔽了中国的 IP 地址,连国内的 Visa 信用卡也一同屏蔽,即 ...

  2. QML中border、padding、margin

    1.border 定义 border代表边框,可以设置border的宽度和颜色等属性 实现image 实现code Rectangle { width: 200 height: 160 anchors ...

  3. Mybatis 获取自增主键 useGeneratedKeys与keyProperty解答

    Mybatis 获取自增主键 31bafebb-a95b-4c35-a949-8bc335ec6e2e 今天开发的时候遇到一个疑惑,业务场景是这样的, 但是百度好久没有找到合适的解答,于是自己向同事了 ...

  4. LAL v0.34.3发布,G711音频来了,Web UI也来了

    Go语言流媒体开源项目 LAL 今天发布了v0.34.3版本. LAL 项目地址:https://github.com/q191201771/lal 老规矩,简单介绍一下: ▦ 一. 音频G711 新 ...

  5. SQL优化(一)

    1.什么是SQL优化 SQL语句的优化是将性能低下的SQL语句转换成目的相同但是性能优异的SQL语句. 2.为什么需要学习SQL优化 SQL语句是对数据库进行操作的惟一途径,对数据库系统的性能起着决定 ...

  6. 【LeetCode】3.19 对称二叉树

    101. 对称二叉树 ​ 给你一个二叉树的根节点 root , 检查它是否轴对称. 示例 1: 输入:root = [1,2,2,3,4,4,3] 输出:true 示例 2: 输入:root = [1 ...

  7. cf1774f解题报告

    Magician and Pigs 分析一下三个操作分别干了些什么 新添一只猪 使血量为 \(x\) 的猪血量变为 \(\max(x-v,0)\) 设前面操作后猪总共会受到 \(s\) 的伤害,复制一 ...

  8. 2022-11-01:给定一个只由小写字母和数字字符组成的字符串str。 要求子串必须只含有一个小写字母,数字字符数量随意。 求这样的子串最大长度是多少?

    2022-11-01:给定一个只由小写字母和数字字符组成的字符串str. 要求子串必须只含有一个小写字母,数字字符数量随意. 求这样的子串最大长度是多少? 答案2022-11-01: 经典的滑动窗口问 ...

  9. 2022-03-16:给你一个整数 n ,表示有 n 个专家从 0 到 n - 1 编号。 另外给一个下标从 0 开始的二维整数数组 meetings , 其中 meetings[i] = [xi,

    2022-03-16:给你一个整数 n ,表示有 n 个专家从 0 到 n - 1 编号. 另外给一个下标从 0 开始的二维整数数组 meetings , 其中 meetings[i] = [xi, ...

  10. windows-重启打印服务

    @echo off color a net stop spooler net start spooler ping -n 4 localhost >nul