【LeetCode】Find Pivot Index #724 Rust Solution
给定一个整数类型的数组 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的更多相关文章
- 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 【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 ...
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
随机推荐
- 随机服务系统模拟—R实现(三)
M/M/c随机服务系统的模拟 M/M/1服务系统:(1)队列长度没有限制:(2)顾客到达的时间间隔和服务时间均服从指数分布:(3)服务台数量为c. 一.M/M/c随机服务系统的模拟 在M/M/c排队系 ...
- 多线程socketserver
模块:socketserver tcp协议: 服务端: import socketserver class MyRequestHandle(socketserver.BaseRequestHandle ...
- SpringBoot @Target、@Retention、@Documented注解简介
jdk1.5起开始提供了4个元注解:@Target.@Retention.@Documented.@Inherited.何谓元注解?就是注解的注解. 在程序开发中,有时候我们需要自定义一个注解,这个自 ...
- CommunityToolkit.Mvvm8.1 消息通知(4)
本系列文章导航 https://www.cnblogs.com/aierong/p/17300066.html https://github.com/aierong/WpfDemo (自我Demo地址 ...
- 【SpringMVC】(一)
SpringMVC简介 SpringMVC是Spring的一个后续产品,是Spring的一个子项目 基于原生的Servlet,通过了功能强大的DispatcherServlet,对请求和响应进行统一处 ...
- [Asp.Net Core] 网站中的XSS跨站脚本攻击和防范
漏洞说明: 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往 ...
- .NET实现解析字符串表达式
一.引子·功能需求 我们创建了一个 School 对象,其中包含了教师列表和学生列表.现在,我们需要计算教师平均年龄和学生平均年龄. //创建对象 School school = new School ...
- SqlServer 添加字段说明、表说明
1.添加表说明 EXECUTE sp_addextendedproperty N'MS_Description','表说明',N'user',N'dbo',N'table',N'表名',NULL,NU ...
- 前端获取不到环境变量NODE_ENV
有时候我们期望通过执行不同的 npm script 来区分诸如 dev.prod.uat.sit等多环境下使用的不同变量 今天我也在整环境变量,碰到一个小小的bug.装了 cross-env 但还是没 ...
- Costura.Fody 使用问题
1. Costura.Fody 引用后,未能正常合并资源文件.用着用着就不行了 解决方案:在csproj所在的文件目录,找到FodyWeavers.xml,添加<Costura/> 1 & ...