leetcode548 Split Array with Equal Sum
思路:
使用哈希表降低复杂度。具体来说:
枚举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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- [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 ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- 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 ...
随机推荐
- 模块讲解---os模块,sys模块,json和pickle模块,logging模块
目录 模块的用法 os模块 常用的功能 sys模块 常用的功能 json和pickle模块 4. logging模块 模块的用法 通过 import 或者from......import...... ...
- Python3之使用Crypto
pip3 install pycryptodome 快速方式:pip3 install -i https://pypi.douban.com/simple pycryptodome PyCrypto ...
- python自动华 (十六)
Python自动化 [第十六篇]:JavaScript作用域和Dom收尾 本节内容: javascript作用域 DOM收尾 JavaScript作用域 JavaScript的作用域一直以来是前端开发 ...
- Java中通过Array.sort()对数组从大到小排序
package com.itheimajavase; import java.util.Arrays; import java.util.Comparator; public class Day01 ...
- day42_Oracle学习笔记_01
一.Oracle Database 的基本概念 1.1.一个Oracle服务器 详解如下: 一个Oracle服务器是一个关系型数据管理系统(RDBMS),它提供开放的,全面的,近乎完整的信息管理. ...
- mouseenter([[data],fn])
mouseenter([[data],fn]) 概述 当鼠标指针穿过元素时,会发生 mouseenter 事件.该事件大多数时候会与mouseleave 事件一起使用.广州大理石机械构件 与 mous ...
- ODBC连接到400
1.首先iSeries Client安装的时候要勾选ODBC , 这样才能找到Driver 2.某个Application是32位上,要用32位路径下的ODBC Administration打开,添加 ...
- 参数类型 (@Test层)常用参数
long lNum = 1234L; float fNum = 1.23f; double dNum = 1.23d;
- oracle 常用工具类及函数
j_param json; jl_keys json_list; -- 创建json对象j_param j_param := json(p_in_str); -- 校验param域是否缺少必填参数 j ...
- Django module
1,模型定义 models.py的例子: class Author(models.Model): name=models.CharField(max_length=20) class Book(mod ...