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 ...
随机推荐
- bate阶段项目总结
Beta里程碑总结 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 要解决的问题是如何实现消息的发布与查看以及个人主页的实现:定义的基本清楚,团队 ...
- HackPwn2015:IoT智能硬件安全威胁分析
HackPwn2015:IoT智能硬件安全威胁分析 360安全卫士 · 2015/08/26 14:43 IoT(物联网)是一种既危险又有趣的混合技术,所谓的混合技术包括移动应用程序.蓝牙.Wi-Fi ...
- 关于近期项目代码整理(iOS)
近期对项目中所经常使用到的封装代码进行整理,并将其上传至网络保存,本人会在后期不间断的更新其内容.具体链接地址为代码封装 关于代码 这些代码为从学习iOS来到现在实际项目开发中,精炼出来的封装代码,使 ...
- Node 编程规范
空格, 2个, 因为有多层函数嵌套 行宽, 80个字符 语句分隔, 一律用; 并且占一行 变量定义, 每一个都要用 var, 并且不要定义全局变量 变量名和属性名, 小驼峰, yourName 函数, ...
- Linux json解析jq
jq是一个linux下很方便的json解析器,平时看json文件都是一行混在一起的,非常不适合阅读. https://stedolan.github.io/jq/download/ 直接到官网上下载, ...
- JQuery_处理元素和浏览器的窗口的尺寸
一.jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包括内边距.边框或 ...
- js跨域_jsonP
$.ajax("url", type:"get", dataType:"jsonp", jsonp:"callback" ...
- 【cpp】Vector
这vector 很有用 // compile with: /EHsc #include <vector> #include <iostream> int main() { us ...
- webstorm自动编译typescript
http://bbs.egret.com/thread-1752-1-1.html http://bbs.egret.com/thread-1912-1-1.html
- css实现定高的元素在不定高的容器中水平垂直居中(兼容IE8及以上)
容器设置相对定位 元素设置宽高,并使用绝对定位,上下左右值均为0,margin:auto 如下所示: <!DOCTYPE html> <html> <head lang= ...