思路:

使用哈希表降低复杂度。具体来说:

枚举j:

枚举i,如果sum[i - 1] == sum[j - 1] - sum[i],就用哈希表把sum[i - 1]记录下来;

枚举k,如果sum[k - 1] - sum[j] == sum[n - 1] - sum[k]并且哈希表中存在sum[k - 1] - sum[j],返回true。

返回false。

实现:

 class Solution
{
public:
bool splitArray(vector<int>& nums)
{
int n = nums.size();
if (n < ) return false;
vector<int> sum(n, );
sum[] = nums[];
for (int i = ; i < n; i++) sum[i] = sum[i - ] + nums[i];
for (int j = ; j <= n - ; j++)
{
unordered_set<int> st;
for (int i = ; i < j - ; i++)
{
if (sum[i - ] == sum[j - ] - sum[i]) st.insert(sum[i - ]);
}
for (int k = j + ; k <= n - ; k++)
{
if (sum[k - ] - sum[j] == sum[n - ] - sum[k] && st.count(sum[k - ] - sum[j]))
return true;
}
}
return false;
}
}

leetcode548 Split Array with Equal Sum的更多相关文章

  1. [LeetCode] 548. Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  2. LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  3. [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  4. 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  5. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  6. [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  7. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. [Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  9. 698. Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

随机推荐

  1. postgresql backup

    #!/bin/sh # Database backup script # Backup use postgres pg_dump command: # pg_dump -U <user> ...

  2. Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)

    Spring Boot 之FilterRegistrationBean  --支持web Filter 排序的使用Spring 提供了FilterRegistrationBean类,此类提供setOr ...

  3. Oracle 后台进程(五)SMON进程

    转载自:刘相兵 Maclean Liu 文章 你所不知道的后台进程 SMON 功能   SMON(system monitor process)系统监控后台进程,有时候也被叫做 system clea ...

  4. phpweb文件上传下载

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...

  5. HDOJ->考新郎(sdut1021)

    考新郎 Problem Description 在一场盛大的集体婚礼中,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每 ...

  6. css偷懒神奇

    偷懒神奇链接:https://qishaoxuan.github.io/css_tricks/glass/

  7. LR性能测试课程及视频教程

    LR性能测试课程及视频教程课程如下: 1.性能测试核心技术-2.性能测试脚本开发-3.LR场景设计-4.LR指标分析. 1.性能测试是通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各 ...

  8. 关系型数据库的树形结构查询(Oracle、Postgres)

    Oracle : start with… connect by 的用法.语法:select * from table [where 条件1] connect by[条件2] start with[条件 ...

  9. H264基础简介

    前言 H264是属于视频的编码层的标准格式,视频编码显然是为了压缩大小.我们看下一个完全没压缩的视频数据大小.假设视频是高清(1280 * 720),每秒30帧,也就是每秒的数据 1280 * 720 ...

  10. HTTP之缓存

    1. 保持副本的新鲜 HTTP 有一些简单的机制可以在不要求服务器记住有哪些缓存拥有其文档副本的情况下,保持已缓存数据与服务器数据之间充分一致.HTTP 将这些简单的机制称为文档过期(document ...