leetCode练习(15)
2016-09-15 15:16
399 查看
题目:3Sum
难度:medium
问题描述:
Given an array S of n integers, are there elements a,
b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
解题思路:
先对数组进行排序可以极大的减少枚举次数。同时,我是先做了18题~是15题的提高版,因此直接用了18题的方法偷工取巧。具体代码如下:
难度:medium
问题描述:
Given an array S of n integers, are there elements a,
b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
解题思路:
先对数组进行排序可以极大的减少枚举次数。同时,我是先做了18题~是15题的提高版,因此直接用了18题的方法偷工取巧。具体代码如下:
public class Solution { public List> threeSum(int[] nums) { ArrayList> res=new ArrayList>(); int len=nums.length; if(nums==null||len<3){ return res; } Arrays.sort(nums); int max=nums[len-1]; if(nums[0]>0||max<0){ return res; } int i,z; for(i=0;i0&&z==nums[i-1]) continue; if((z+2*max)<0) continue; if(z>0) break; if(z==0){ if((i+2)> foursumlist,int z1){ if(low>=high) return; if(2*nums[low]>target||2*nums[high]target){ j--; } } return; } }
相关文章推荐
- leetcode 练习(15,355)
- leetCode练习(42)
- The Solution to LeetCode 15 3Sum改进版
- [LeetCode] 15 - 3Sum
- week15-leetcode #122-BestTimetoBuyandSellStock II
- 面试题15:链表中倒数第k个节点(Leetcode-19:Remove Nth Node From End of List)
- leetcode练习
- 算法练习_LeetCode_链表1
- leetcode练习 Network Delay Time
- [LeetCode-15]Binary Tree Zigzag Level Order Traversal
- LeetCode15:3Sum
- LeetCode - 15. 3Sum
- python练习程序(c100经典例15)
- leetCode练习(63)
- leetCode练习(72)
- leetCode练习(75)
- Leetcode#15||3 Sum
- leetcode --15. 3Sum
- linux学习笔记15-linux基础练习二
- c语言练习 10-1. 在字符串中查找指定字符(15)