首页 > 其他分享 >leetcode-1013-easy

leetcode-1013-easy

时间:2023-05-18 20:35:27浏览次数:41  
标签:arr end int sum begin easy false leetcode 1013

Partition Array Into Three Parts With Equal Sum

Given an array of integers arr, return true if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes i + 1 < j with (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])

Example 1:

Input: arr = [0,2,1,-6,6,-7,9,1,2,0,1]
Output: true
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
Example 2:

Input: arr = [0,2,1,-6,6,7,9,-1,2,0,1]
Output: false
Example 3:

Input: arr = [3,3,6,5,-2,2,5,1,-9,4]
Output: true
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
Constraints:

3 <= arr.length <= 5 * 104
-104 <= arr[i] <= 104

思路一:数组分三段,分别统计这三段的值,注意每段求和的时候需要提前赋值

    public static boolean canThreePartsEqualSum(int[] arr) {
        int sum = 0;
        for (int i : arr) {
            sum += i;
        }

        if (sum % 3 != 0) {
            return false;
        }

        int val = sum / 3;

        int begin = 0;
        int end = arr.length - 1;

        int partOne = arr[begin++];
        while (partOne != val && begin <= end) {
            partOne += arr[begin];
            begin++;
        }

        int partThree = arr[end--];
        while (partThree != val && end >= 0) {
            partThree += arr[end];
            end--;
        }

        if (begin > end) {
            return false;
        }

        int parTwo = arr[begin++];
        while (begin <= end) {
            parTwo += arr[begin];
            begin++;
        }

        return partOne == parTwo && parTwo == partThree;
    }

思路二:看了一下题解,中间那段不用计算,如果首尾的和相等,中间那段也一定相等


标签:arr,end,int,sum,begin,easy,false,leetcode,1013
From: https://www.cnblogs.com/iyiluo/p/17413212.html

相关文章

  • leetcode-1128-easy
    NumberofEquivalentDominoPairsGivenalistofdominoes,dominoes[i]=[a,b]isequivalenttodominoes[j]=[c,d]ifandonlyifeither(a==candb==d),or(a==dandb==c)-thatis,onedominocanberotatedtobeequaltoanotherdomino.R......
  • leetcode-1422-easy
    MaximumScoreAfterSplittingaStringGivenastringsofzerosandones,returnthemaximumscoreaftersplittingthestringintotwonon-emptysubstrings(i.e.leftsubstringandrightsubstring).Thescoreaftersplittingastringisthenumberofze......
  • leetcode-1295-easy
    FindNumberswithEvenNumberofDigitsGivenanarraynumsofintegers,returnhowmanyofthemcontainanevennumberofdigits.Example1:Input:nums=[12,345,2,6,7896]Output:2Explanation:12contains2digits(evennumberofdigits).345cont......
  • leetcode-1103-easy
    DistributeCandiestoPeopleWedistributesomenumberofcandies,toarowofn=num_peoplepeopleinthefollowingway:Wethengive1candytothefirstperson,2candiestothesecondperson,andsoonuntilwegivencandiestothelastperson.Th......
  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......
  • #yyds干货盘点# LeetCode程序员面试金典:从中序与后序遍历序列构造二叉树
    题目:给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。 示例1:输入:inorder=[9,3,15,20,7],postorder=[9,15,7,20,3]输出:[3,9,20,null,null,15,7]示例2:输入:inorder=[-1],postorder......
  • 动态规划算法基础及leetcode例题
    01基础理论题型:动规基础(斐波那契数列or爬楼梯);背包问题;打家劫舍;股票问题;子序列问题动规误区:只要看懂递推就ok(递推公式只是一部分)解决动态规划应该要思考的几步:状态转移的DP数组以及下标的含义递推公式DP数组为何初始化遍历顺序打印DP数组02例题基础题目509.斐波那......
  • leetcode:二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。题目链接:104.二叉树......
  • Leetcode-Easy 806. Number of Lines To Write String
    题目描述给一个字符串S,从左到右将它们排列行,每行最大长度为100,,同时给定一个数组withds,widths[0]对应着a的宽度,widths[1]对应着b的宽度,...,widths[25]对应着z的宽度。求:至少需要多少行以及最后一行的长度下面是一个实例:Example:Input:widths=[10,10,10,10,10,10,1......
  • leetcode 1321 餐館營業額變化增長
    leetcode1321餐館營業額變化增長selectdistinctc2.visited_on,(selectsum(amount)fromCustomerc1wherec1.visited_on<=c2.visited_onandc1.visited_on>=date_sub(c2.visited_on,interval6day))asamount,round((selectsum(amount)fromCustomerc1......