Leetcode Array 724 FindPivotIndex
来源:互联网 发布:型材吹淘宝怎么搜 编辑:程序博客网 时间:2024/06/03 09:10
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: 3Explanation: 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: -1Explanation: There is no index that satisfies the conditions in the problem statement.
Note:
nums
will be in the range [0, 10000]
.nums[i]
will be an integer in the range [-1000, 1000]
.我的做法:超时了
class Solution(object): def pivotIndex(self, nums): """ :type nums: List[int] :rtype: int """ length = len(nums) for index in range(0,length): if sum(nums[0:index])==sum(nums[index+1:length]): return index return -1百度到的做法:思路:检验是否符合pivotIndex成立时候的特征
class Solution(object): def pivotIndex(self, nums): """ :type nums: List[int] :rtype: int """ sums = sum(nums) total = 0 for x, n in enumerate(nums): if sums - n == 2 * total: return x total += n return -1
阅读全文
0 0
- Leetcode Array 724 FindPivotIndex
- [leetcode][array] Rotate Array
- LeetCode:Array
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- Array-Leetcode
- LeetCode Array
- Leetcode-array
- leetcode array
- [leetcode]Array
- mybatis反向生成代码工具
- vb mid函数的使用方法详细介绍
- 求n个字符串的最大相同子串
- 从 HTTP 到 HTTPS 再到 HSTS
- $state.go跳转中 传递对象参数
- Leetcode Array 724 FindPivotIndex
- 一步一步带你认识MVP+Retrofit+Rxjava并封装(一)
- hdoj 2063 过山车
- 【Ubuntu】win7+Ubuntu16.04双系统安装与卸载
- opencsv4.0 自定义规则
- idea启动tomcat报错:Artifact mmall:war: Server is not connected. Deploy is not available
- 学习C++笔记(三)
- 【阿里云MVP月度分享】SaaS服务商如何通过数加平台统计业务流量
- SpringMVC使用RedirectAttributes实现错误消息处理