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 ...
随机推荐
- 根据标记清空页面中所有的input对象
function clear1(flag) { //获取页面中所有的input对象 var inputs = document.getElementsByTagName("input&quo ...
- jQuery 遍历方法
http://www.runoob.com/jquery/jquery-ref-traversing.html
- startActivity跳转失败而且没有异常信息
startActivity跳转不能显示目标activity的布局(显示空白页),而且没有异常信息 onCreate()方法重写错误 应该重写的是onCreate(Bundle savedInstanc ...
- Python_Day7_面向对象学习
1.面向对象编程介绍 2.为什么要用面向对象进行开发? 3.面向对象的特性:封装.继承.多态 4.类.方法. 面向过程 VS 面向对象 编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告 ...
- samba完美安装
感觉是一个相当强大的东西. Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件.它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务.为客户机/服务器型协议,客户机通过该协议 ...
- unity-点乘和叉乘的应用
http://blog.csdn.net/oskytonight/article/details/38900087 点乘:两个向量点乘得到一个标量 ,数值等于两个向量长度相乘后再乘以二者夹角的余弦值 ...
- 获取$(this)子节点对象的方法
获取$(this)子节点对象的方法: 1.children()方法: children() 方法返回被选元素的所有直接子元素. 该方法只会向下一级对 DOM 树进行遍历. 2.find()方法: fi ...
- 【adb】adb基本命令总结
adb常用基本命令如下: adb devices 列出你的devices aapt dump badging <file_path.apk> 查看包名 adb ...
- git代码冲突解决
1.git fetch 跟git pull差别是前者不会和本地直接merge code,而后者会,所以git fetch更安全 git fetch origin master:tmpgit dif ...
- python 学习2
在1的基础上 settings.py blog/models.py: 打开一个cmd, net start mysql ,启动mysql服务 mysql -uroot -p 再在py_fir目录下打开 ...