Leetcode724:寻找数组的中心索引(java、python3)
寻找数组的中心索引
给定一个整数类型的数组 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]
的整数。
起先我的思路是从第一个索引左累加、右累加判断是否想等:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(); int sumLeft=0,sumRight=0; Scanner scan =new Scanner(System.in); while (scan.hasNextInt()){ nums.add(scan.nextInt()); } int numsSize=nums.size()-1; for(int i=1;i<numsSize;i++){ for(int j=0;j<i;j++){ sumLeft+=nums.get(j); } for(int j=numsSize;j>i;j--){ sumRight+=nums.get(j); } if(sumLeft==sumRight){ System.out.println(i); break; }else { sumLeft=sumRight=0; } } if(sumLeft==0){ System.out.println(-1); } } }
做完之后搜一下别人的答案。。。果然被吊打。
解题思路:
左累加没必要从0号索引开始,之前的累加加上下一号索引即可。右累加完全可以算一次总和减去左累加即可。
参考:判断中心索引条件
左侧索引如果和右侧相等,即二倍加左侧累加和+中心索引=总和
java:
class Solution { public int pivotIndex(int[] nums) { int sumLeft=0,sum=0; for (int n:nums){ sum = sum + n; } int numsSize=nums.length-1; for(int i=0;i<=numsSize;i++){ if(i==0){ sumLeft=0; }else{ sumLeft+=nums[i-1]; } //左侧索引如果和右侧相等,即二倍左侧加中心索引=总和 if(sumLeft*2+nums[i]==sum){ return i; } } return -1; } }
特别说明一下,索引一定要从0开始到最后一个止,因为题目后中心索引右侧相加可以为0。
java默认模板里int[] nums
指从控制台输入int 数组,不需要Arraylist动态构造数组了。
python3
nums即为list动态数组
class Solution: def pivotIndex(self, nums: List[int]) -> int: """ :type nums: int """ sumAll=sum(nums) leftSum=0 for i in range(len(nums)): if(i==0): leftSum=0 else: leftSum+=nums[i-1] #左侧索引如果和右侧相等,即二倍加左侧中心索引=总和 if(leftSum*2+nums[i]==sumAll): return i return -1
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java并发编程,深度探索J.U.C - AQS
java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.U.C 的核心。 CountdownLatch用来控制一个线程等待多个线程。 维护了一个计数器 cnt,每次调用 countDown() 方法会让计数器的值减 1,减到 0 的时候,那些因为调用 await() 方法而在等待的线程就会被唤醒。public class CountdownLatchExample { public static void main(String[] args) throws InterruptedException { final int totalThread = 10; CountDownLatch countDownLatch = new CountDownLatch(totalThread); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < totalThread; i++) { executorService.execut...
- 下一篇
Leetcode747至少是其他数字两倍的最大数
Leetcode747至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 。查找数组中的最大元素是否至少是数组中每个其他数字的两倍。如果是,则返回最大元素的索引,否则返回-1。 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. 示例 1: 输入: nums = [3, 6,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器