【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 ...
随机推荐
- odoo 开发入门教程系列-准备一些操作(Action)?
准备一些操作(Action)? 到目前为止,我们主要通过声明字段和视图来构建模块.在任何真实的业务场景中,我们都希望将一些业务逻辑链接到操作按钮.在我们的房地产示例中,我们希望能够: 取消或将房产设置 ...
- How to implement UDP protocal
Server implementation Open a socket on the server that listens to the UDP requests. (I've chosen 888 ...
- [Python]【Form Data vs Request Payload】之 python 爬虫如何实现 POST request payload 形式的请求
1 问题描述 欲使用Python的requests库(requests.session().request(...))实现对此Ajax的POST请求进行模拟实现. 但在chrome发现其请求的形式不一 ...
- 基于SpringBoot实现单元测试的多种情境/方法(二)
本文分享自天翼云开发者社区@<基于SpringBoot实现单元测试的多种情境/方法(二)>, 作者:才开始学技术的小白 1 Mock基础回顾 在上一篇分享中我们详细介绍了简单的.用moc ...
- Idea快捷键——查找源码
双击shift 输入要查找源码类 相当于查 java_jdk_chm Ctrl+F12 :浏览类
- 3D开发工具HOOPS最新解析合集!助力实现web端高性能模型渲染!
一.3D技术为创新提供强大助力(1)3D专家提供专属技术支持服务不管您想搭建桌面.WEB或者移动端APP应用,技术领先全球的HOOPS Platform组件都可以为您提供弹性的3D集成架构,同时,一批 ...
- JQuery点击复制文本框内容的方法插件
[导读] 文章介绍了两种常用的点击复制文本框内容方法,一种是but IE only,同样的这个也是我们经常使用的.优点是体积小,仅有十来行代码,但缺点也很明显,只支持IE及以IE为内核的浏览器,另一种 ...
- msp430点灯实验
title: msp430点灯实验 date: 2023-04-15 15:31:25 description: 基于msp430f5529点灯实验 一.实验内容 使用开发板:msp430f5529 ...
- springboot mybatis 动态调用oracle存储过程,通过存储过程名称,就能动态调用存储过程、java动态调用oracle存储过程
由于在开发业务时,可能同时调用的存储过程不知道参数,但是参数从界面.或已经存储在数据库的获取,所以就不希望手动写存储过程的参数,通过简化的调用. 能不能写个动态的业务,只输入存储过程名称,自动获取存储 ...
- Python_14 接口测试报告
一.查缺补漏 1. 测试用例要复制到pycharm执行的项目中,才能显示 2. 函数用下划线,类用大驼峰 3. pycharm一行显示(不换行): File -> settings-> E ...