[LeetCode]Find Pivot
Given an array of integers nums, write a method that returns the “pivot” index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
Example 1:
Input:
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
Example 2:
Input:
nums = [1, 2, 3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.
class Solution {
public:
int pivotIndex(vector<int>& nums) {
if(nums.size()<3) return -1;
int sum[nums.size()], max;
for(int i = 0; i < nums.size(); i ++){
i == 0?sum[i] = nums[i] : sum[i] = nums[i] + sum[i-1];
}
max = sum[nums.size()-1];
for(int i = 0; i < nums.size(); i++){
if(max - sum[i] == sum[i] - nums[i]){
return i;
}
}
return -1;
}
};
[LeetCode]Find Pivot的更多相关文章
- [LeetCode] Find Pivot Index 寻找中枢点
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- LeetCode算法题-Find Pivot Index(Java实现)
这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...
- Python解Leetcode: 724. Find Pivot Index
leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- [Leetcode]724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- OWASP JUICE SHOP部分题解
本文作者:S0u1 0×00 简介 OWASP JUICE SHOP是一个开源的web应用靶场,里面包含了共记47个漏洞挑战任务,囊括了OWASP TOP 10的各个点,是一个很不错的渗透测试练手项目 ...
- 使用X-Pack插件来进行权限控制
1.为elasticsearch安装X-Pack插件.进入 elasticsearch根目录,执行: bin/elasticsearch-plugin install x-pack杀掉进程,重启es, ...
- React 组件模式
简评:组件(component)是 React 的核心,了解它们有助于构建好的设计结构. 什么是组件(component) 组件运行你将 UI 拆分为独立的可重用的部分.和 JavaScript 函数 ...
- 结合业务,精炼SQL
现代网站,性能的瓶颈都围绕着数据库的性能来谈.数据库是存储的核心部件,在日益增长的流量中会凸显数据库的性能瓶颈.从<淘宝技术十年>书中来看,淘宝发展历程中从MYSQL换成了ORACLE又换 ...
- UVALive-7457-Discrete Logarithm Problem(取模运算)
原题链接 额,一直在理解题意在纠结看不懂,后来才恍然大悟 题意:定义一种新运算 a × b = a * b mod p : 已知条件给定一个p 求 x 这里用到同余与模运算乘法公式:a * b % n ...
- 数据结构基础 ---- 数组的理解和实现(Java)
什么是数组 数组是由类型相同的数据元素构成的有序集合,每个元素称为数组元素,每个元素受n(n>= 1)个线性关系的约束,每个元素在n个线性关系中的序号i1, i2, ....., in称该元素的 ...
- 2016级算法期末模拟练习赛-B.AlvinZH的青春记忆I
1083 AlvinZH的青春记忆I 思路 中等题,动态规划. 简化题意,一个环上取数,数不可相邻,取取得数之和最大值. 环不好表示,可以解开变成一列数,那么答案应为下列两种情况较大者. ①:取第一个 ...
- 【APUE】第3章 文件I/O (1)
1.文件描述符 对于内核来说,所有打开的文件都通过文件描述符来引用.文件描述符是一个非负整数.当打开一个现有的文件或者创建一个新文件时,内核向进程返回一个文件描述符.当读.写一个文件时,使用open或 ...
- python中TAB补全
tab补全的代码文件tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcom ...
- ArrayList分析
ArrayList概述 ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList ...