leetcode 日记 4sum java
整体思路同之前的一样,依然采取降低维度的方式进行
public List<List<Integer>> solution(int nums[], int target) {
List<List<Integer>> result = new ArrayList<>();
if((nums.length<4)||(nums==null))
{
return result;
}
Arrays.sort(nums);
if ((4*nums[0]>target)||(4*nums[nums.length-1]<target))
{
return result;
}//上面两个队特例的快速结束一定要加
int N = nums.length;
for (int i = 0; i < N; i++) {
int[] temnum = new int[N - 1];
System.arraycopy(nums, 0, temnum, 0, i);
System.arraycopy(nums, i + 1, temnum, i, N - i - 1);
result = threeSum(temnum, target - nums[i], nums[i], result);
}
return result;
}
public List<List<Integer>> threeSum(int[] nums, int target, int num,List<List<Integer>> result) {
int sum;
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end) {
sum = nums[i] + nums[start] + nums[end];
if (sum < target) {
start++;
continue;
}
if (sum > target) {
end--;
continue;
}
//下面的部分为对每一结果内部进行进行排序,这样在去重时方便
if (sum == target) {
List<Integer> list = new ArrayList<>();
if(num<nums[i])
list.add(num);
list.add(nums[i]);
if(num>=nums[i] && num<nums[start])
list.add(num);
list.add(nums[start]);
if ( num>=nums[start] &&num<nums[end])
list.add(num);
list.add(nums[end]);
if (num>=nums[end])
list.add(num);
start++;
end--;
if (result.contains(list))
continue;
result.add(list);
}
}
}
return result;
}
整体速度在leetcode上大概为50%
leetcode 日记 4sum java的更多相关文章
- leetcode 日记 3sumclosest java
思路一为遍历: public int thirdSolution(int[] nums, int target) { int result = nums[0] + nums[1] + nums[2]; ...
- leetcode 18 4Sum JAVA
题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
- [LeetCode] 454. 4Sum II 四数之和II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
随机推荐
- CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
以下全部转载于 http://blog.csdn.net/lane_l/article/details/20235909 本人于今晚按照该文章使用centos 6.7 64bit安装成功,做个备份, ...
- 单例模式getInstance()
/** * 对象的实例化方法,也是比较多的,最常用的方法是直接使用new,而这是最普通的,如果要考虑到其它的需要,如单实例模式,层次间调用等等. * 直接使用new就不可以实现好的设计好,这时候需要使 ...
- html5拖拽总结
拖拽(Drag 和 drop)是 HTML5 标准的组成部分.拖拽是一种常见的特性,即抓取对象以后拖到另一个位置. Internet Explorer 9.Firefox.Opera 12.Chrom ...
- asp.net mvc4 过滤器的简单应用:登录验证
直接上代码,不要说话. ASP.NET MVC4过滤器的简单应用:验证登录 [AcceptVerbs(HttpVerbs.Post)] public ActionResult login(FormCo ...
- iOS - Share 分享/第三方登录
1.系统方式创建分享 按照下图在 Info.plist 文件中将 Localization native development region 的值改为 China.如果不设置此项弹出的分享页面中显示 ...
- 泛型数组列表 ArrayList
为什么使用泛型数组列表而不使用普通数组? 1.普通数组经常会发生容量太大以致浪费的情况 2.普通数组无法动态更改数组 基本概念: 1.采用[类型参数]的[类]---->[泛型类] 2.[泛型类型 ...
- angularJS 学习之路
AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 AngularJS 应用程序.也就是angularjs作用的入口 作用在什么标签或者整个body ...
- Asp.Net_Mvc_@Html.xxx()的扩展
/// <summary> /// 生成分类下拉-列表框,选中指定的项 /// </summary> /// <param name="html"&g ...
- TAP/TUN摘要
TUN适用于IP帧.Tap适用于以太网帧.TAP摸拟一个以太网设备(以arp广播MAC识别),它操作第二层数据包如以太网数据帧.TUN模拟了网络层ip设备(以点对点的方式,使用ip标识),操作第三层数 ...
- ubuntu 跟xshell的问题
有2个分析: 1:是windos的防火墙没有关闭 2:是虚拟机没有安装sshd服务器 ubuntu在CLI界面下输入:dpkg -l |grep ssh 因为是我安装过的sshd server 要 ...