454 四数相加
另一道两数之和的题目
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
for(int num1 : nums1) {
for(int num2 : nums2) {
int count = map.getOrDefault(num1 + num2, 0) + 1;
map.put(num1 + num2, count);
}
}
int count = 0;
for(int num3 : nums3) {
for(int num4 : nums4) {
if (map.containsKey(0 - num3 - num4)) {
count += map.get(0 - num3 - num4);
}
}
}
return count;
}
}
383 赎金信
很简单的hash数据结构应用
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> map = new HashMap<>();
for (char c : magazine.toCharArray()) {
int count = map.getOrDefault(c, 0) + 1;
map.put(c, count);
}
for(char c : ransomNote.toCharArray()) {
if (map.containsKey(c)) {
int count = map.get(c) - 1;
if (count == 0) {
map.remove(c);
} else {
map.put(c, count);
}
} else {
return false;
}
}
return true;
}
}
15 三数之和
使用的是双指针法,这个题目看labuladong的更易看懂:labuladong一个方法团灭 nSum 问题
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
return threeSum(nums, 0);
}
public List<List<Integer>> threeSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0 ; i < nums.length; i++) {
List<List<Integer>> tuples = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> tuple : tuples) {
tuple.add(nums[i]);
res.add(tuple);
}
while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
public List<List<Integer>> twoSum(int[] nums, int start, int target) {
List<List<Integer>> res = new ArrayList<>();
int leftIndex = start, rightIndex = nums.length - 1;
while(leftIndex < rightIndex) {
int sum = nums[leftIndex] + nums[rightIndex];
int left = nums[leftIndex], right = nums[rightIndex];
if (sum < target) {
while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
} else if (sum > target) {
while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
} else {
// 注意此处不能使用Arrays.asList(),不然会导致UnsupportedOperationException
List<Integer> tuple = new ArrayList<>();
tuple.add(nums[leftIndex]);
tuple.add(nums[rightIndex]);
res.add(tuple);
while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
}
}
return res;
}
}
UnsupportedOperationException异常的解决方法
18 四数之和
注意target范围,换成long类型
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; i++) {
List<List<Integer>> triples = threeSum(nums, i + 1, target - nums[i]);
for (List<Integer> triple : triples) {
triple.add(nums[i]);
res.add(triple);
}
while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
public List<List<Integer>> threeSum(int[] nums, int start, long target) {
List<List<Integer>> res = new ArrayList<>();
for(int i = start ; i < nums.length; i++) {
List<List<Integer>> tuples = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> tuple : tuples) {
tuple.add(nums[i]);
res.add(tuple);
}
while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
public List<List<Integer>> twoSum(int[] nums, int start, long target) {
List<List<Integer>> res = new ArrayList<>();
int leftIndex = start, rightIndex = nums.length - 1;
while(leftIndex < rightIndex) {
int sum = nums[leftIndex] + nums[rightIndex];
int left = nums[leftIndex], right = nums[rightIndex];
if (sum < target) {
while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
} else if (sum > target) {
while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
} else {
List<Integer> tuple = new ArrayList<>();
tuple.add(nums[leftIndex]);
tuple.add(nums[rightIndex]);
res.add(tuple);
while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
}
}
return res;
}
}