15. 三数之和
强行用哈希表的做法,类似前面的四数相加,这道题最麻烦的就是处理重复和ijk不相等
刚转Java发现排序要用Collections.sort(collection)
对于内容去重可以将元素排序后放到HashSet里面
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Map<Integer,List<Integer>> map = new HashMap<>();
Set<List<Integer>> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i != j) {
List<Integer> l = new ArrayList<>();
l.add(i);
l.add(j);
map.put(nums[i]+nums[j],l);
}
}
}
for (int k = 0; k < nums.length-1; k++) {
if (map.containsKey(-nums[k]) && !(map.get(-nums[k]).contains(k))) {
List<Integer> ll = map.get(-nums[k]);
int l1 = ll.get(0);
int l2 = ll.get(1);
List<Integer> res = new ArrayList<>();
res.add(nums[l1]);
res.add(nums[l2]);
res.add(nums[k]);
Collections.sort(res);
set.add(res);
}
}
return new ArrayList<>(set);
}
}
18. 四数之和
类似刚刚的三数之和,发现去重剪枝太难了,放弃哈希开始双指针。。。
这类n数之和如果用双指针的话,先n-2层for循环nums[i] + nums[j]+......为确定值,然后再用一层左右指针遍历,实现n-1次方的时间复杂度
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0 ; i < nums.length - 1; i++){
if(nums[i] > 0 && target < 0) return res;
if(i > 0 && nums[i] == nums[i -1]) continue;
for(int j = i + 1; j < nums.length;j++){
if( j != i +1 && nums[j] == nums[j -1]){
continue;
}
int left = j + 1;
int right =nums.length - 1;
while(left < right){
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if(sum < target){
left++;
}else if(sum > target){
right--;
}else {
res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right &&nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
}
return res;
}
}
344.反转字符串
哈希表刷完来道练手题,就是实现reverse
顺便查了下数组和集合可以分别调用Arrays和Collections的静态方法reverse
class Solution {
public void reverseString(char[] s) {
int i = 0, j = s.length-1;
while(i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
}