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 ...
随机推荐
- 怎么在vscode里搜索函数
怎么在vscode里搜索函数?在vsCode编辑器中如何跨文件查找函数的定义? 问题: 比如: 在 a.js中使用 var res = window.unique(arr); 在未知的js文件定义了u ...
- stm32如何核对flash大小和sram大小
以stm32f103zet6为例,直接上图:
- 我是如何理解Android的Handler模型_1
Handler Message类似于旧时的电话系统,对应关系如下: 电话局->Handler 电话机->Message 接线员->handlerMessage 接线员的工作-> ...
- C指针的运算
指针的运算在数组中才有意义 int *p; p++,一定是在一片连续的区域才有意义,当然越界也会带来诸多问题. void main() { ; int *p = #//这根本无界可言 ...
- zabbix通过钉钉报警
1.创建报警脚本 vim /usr/local/share/zabbix/alertscripts/dingalert.py #!/usr/bin/env python import json im ...
- linux笔试题
1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统 C. 跟踪管理系统信息和错误 D. 管理系统日常任务的调度 2. 在大多数Linux发行版本 ...
- Material Icons 查找的替代办法
1 问题 1.1 国内访问 google 困难,众所周知 1.2 在平时的工作中使用 Material Icons 如何查询呢 2 办法 2.1 github.com 把包 download 2.2 ...
- 基于ARM的SoC设计入门[转]
原文:基于ARM的SoC设计入门 我们跳过所有对ARM介绍性的描述,直接进入工程师们最关心的问题.要设计一个基于ARM的SoC,我们首先要了解一个基于ARM的SoC的结构.图1是一个典型的SoC的结构 ...
- 百度翻译api初使用(很久没写python了,写几行玩玩)
调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...
- final关键字的理解
final :最终作为一个修饰符 1.可以修饰类,函数,变量: 2.被final修饰的类不可以被继承: 3.被final修饰的方法不可以被复写: 4.被final修饰的变量是一个常量,只能赋值一次,既 ...