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 ...
随机推荐
- Springboot注解@ServletComponentScan和@ComponentScan(转)
一.SpringBoot中使用Servlet在SpringBootApplication上使用@ServletComponentScan注解后,Servlet.Filter.Listener可以直接通 ...
- 002_UCOSIII任务创建于删除
(一)先创建一个启动任务来进行创建其它任务,创建任务的宏定义 #define START_TASK_PRIO 3 //任务优先级 #define START_STK_SIZE 128 //任务堆栈大小 ...
- CKEditor本地图片自动上传插件
由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...
- ipv4保留地址
曾经以为保留地址下面三类.原来使用中还有很多的. A类 10.0.0.0--10.255.255.255 B类 172.16.0.0--172.31.255.255 C类 192.168.0.0--1 ...
- Selenium执行cdp命令,driver.execute_cdp_cmd用法
Chrome自带的开发者工具DevTools功能非常强大.有时候我们在使用Selenium操作浏览器时需要通过调用一下DevTools的方法来完成一些设置,如模拟移动设备,弱网模拟等等. Seleni ...
- Fiddler 安装使用
Fiddler是一个蛮好用的抓包工具,可以将网络传输发送与接受的数据包进行截获.重发.编辑.转存等操作.也可以用来检测网络安全.反正好处多多,举之不尽呀!当年学习的时候也蛮费劲,一些蛮实用隐藏的小功能 ...
- elasticsearch shield在java中的应用
官方文档:https://www.elastic.co/guide/en/shield/current/_using_elasticsearch_java_clients_with_shield.ht ...
- JavaWeb——Get、Post请求中文乱码问题
最近在重温JavaWeb基础内容,碰到了之前也时常遇到的中文乱码问题,想着反正是经常要处理的,不如当即就把它整理出来放在博客里,省得遇到时再去到处搜. 1. Post请求乱码的解决方案: 手工创建一个 ...
- Xshell查看日志
查询日志命令(复制后鼠标右键粘贴): tail -1000f /mnt/logs/SMFManagement/SMFManagement_info.log
- C之堆栈
栈* 自动申请,自动释放* 大小固定,内存空间连续* 从栈上分配的内存叫静态内存 堆* 程序员自己申请* new/malloc* 大小取决于虚拟内存的大小,内存空间不连续* java中自动回收,C中需 ...