leetcode - 4Sum
题目描述:点击此处
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 class Solution { 7 public: 8 vector<vector<int> > fourSum(vector<int> &num, int target) { 9 // Start typing your C/C++ solution below 10 // DO NOT write int main() function 11 vector<vector<int> > result; 12 vector<int> entry; 13 entry.assign(4,target+1); 14 sort(num.begin(),num.end()); 15 vector<int>::iterator i ,j ,k ,l; 16 for (i=num.begin();i<num.end();i++){ 17 if (i>num.begin() && *i==*(i-1)) 18 continue; 19 if (*i+*i+*i+*i>target) 20 break; 21 for (j=i+1; j<num.end();j++){ 22 if (j>i+1 && *j==*(j-1)) 23 continue; 24 if (*i+*j+*j+*j>target) 25 break; 26 for (k=j+1; k<num.end();k++){ 27 if (k>j+1 && *k == *(k-1)) 28 continue; 29 if (*i+*j+*k+*k>target) 30 break; 31 l = lower_bound(k+1,num.end(),target-*i-*j-*k); 32 if (l>=num.end()) 33 continue; 34 if (*i+*j+*k+*l == target) { 35 if (*i!=entry[0]||*j!=entry[1]||*k!=entry[2]||*l!=entry[3]){ 36 entry[0] = *i; 37 entry[1] = *j; 38 entry[2] = *k; 39 entry[3] = *l; 40 result.push_back(entry); 41 } 42 continue; 43 } 44 } 45 } 46 } 47 return result; 48 } 49 };
转载于:https://www.cnblogs.com/zhanghs/archive/2013/03/03/2941830.html
leetcode - 4Sum相关推荐
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- Leetcode: 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- LeetCode 2.1.10 4Sum
2.1.10 4Sum 描述 Given an arraySofnintegers, are there elementsa, b, c, andd inSsuch thata+b+c+d=targe ...
- LeetCode 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- taoqick 搜索自己CSDN博客
L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...
- LeetCode算法入门- 4Sum -day11
LeetCode算法入门- 4Sum -day11 Given an array nums of n integers and an integer target, are there element ...
- 4Sum -- LeetCode
原题链接: http://oj.leetcode.com/problems/4sum/ 这道题要求跟 3Sum 差不多,只是需求扩展到四个的数字的和了.我们还是可以按照 3Sum 中的解法,只是 ...
最新文章
- php图片编辑失真,PHP上传真彩图片缩略图质量失真解决方法
- Postgres多版本控制
- php 怎么支持中文图片显示,php读取中文图片文件读不了,编码有关问题
- HTML-CSS- JavaScript——网页三剑客
- openpyxl方法记录
- python利用集合的无重复性_python集合?
- JQUERY获取DOM
- 【网络流】网络流小总结
- Qt 字符串QString arg()用法总结
- spring boot集成Quartz实现定时任务
- Delphi 7生成XML
- 【oracle11g,17】存储结构: 段的类型,数据块(行连接、行迁移,块头),段的管理方式,高水位线...
- 101与金根回顾敏捷个人:(82)《把时间当做朋友》
- 黑马程序员Java教程学习笔记(一)
- Oracle定时任务使用
- 基于STM32 LCD屏实现的俄罗斯方块小游戏(20220522完成 第一个综合类项目)
- IT运维的365天--009微信双开批处理文件运行出错(当前目录无效)的解决
- 《用计算机画画》教学设计,第8课《简简单单画幅画》教学设计
- html的行高标签是那个,HTML 2-行高、a标签
- Android高工:细说 Android 多线程,探究原理知其所以然
热门文章
- java迭代器的原理_小学生之Java中迭代器实现的原理
- java栈和堆的区别_java 栈 和 堆 的区别
- Struts工作原理
- 学生兴趣爱好管理系统 c语言,《学生兴趣爱好系统.doc
- AI CC2018软件安装教程
- Mybatis的一对一查询以及延迟加载
- mysql port range_Defining port and port-range in /etc/services file
- c语言计算pi后1000位,计算圆周率 Pi (π)值, 精确到小数点后 10000 位
- android对应的java版本_Android Studio gradle插件版本和gradle版本对应关系
- mysqlsql varchar类型只取前四个_Apache Doris0.12.0 发布,进入 Apache 孵化器后第四个正式版本...