LeetCode: 454 四数相加Ⅱ
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> foretwo = new HashMap<Integer, Integer>();
for(int i: nums1){
for(int j: nums2){
foretwo.put(i+j, foretwo.getOrDefault(i+j, 0) + 1);
}
}
int output = 0;
for(int i: nums3){
for(int j: nums4){
output += foretwo.getOrDefault(-i-j, 0);
}
}
return output;
}
}
LeetCode: 383 赎金信
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
for(int i = 0; i < magazine.length(); i++){
char c = magazine.charAt(i);
map1.put(c, map1.getOrDefault(c, 0) + 1);
}
for(int i = 0; i < ransomNote.length(); i++){
char c = ransomNote.charAt(i);
if(!map1.containsKey(c)) return false;
map1.put(c, map1.getOrDefault(c, 0) - 1);
if(map1.get(c)==0) map1.remove(c);
}
return true;
}
}
LeetCode: 15 三数之和
哈希表,有点容易超时,剪枝半天
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> output = new ArrayList<>();
HashSet<Integer> set1 = new HashSet<Integer>();
int len = nums.length;
Arrays.sort(nums);
int firstpos = 0;
for(int i = 0; i < len; i++){
if(nums[i] >= 0){
firstpos = i;
set1.add(nums[i]);
}
}
for(int i = 0; i <= firstpos; i++){
if(i > 0 && nums[i] == nums[i - 1]) continue;
for(int j = i + 1; j < len - 1; j++){
if(j > i + 1 && nums[j] == nums[j - 1]) continue;
int wantk = -nums[i]-nums[j];
if(wantk >= nums[j] && set1.contains(wantk)){
if(wantk == nums[j] && nums[j+1] != nums[j])
continue;
output.add(Arrays.asList(nums[i], nums[j], wantk));
}
}
}
return output;
}
}
还是双指针好
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> output = new ArrayList<>();
int len = nums.length;
Arrays.sort(nums);
for(int i = 0; i < len; i++){
if(nums[i] > 0) return output;
if(i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1;
int right = len - 1;
while(right > left){
int sum = nums[i] + nums[left] + nums[right];
if(sum > 0) right--;
else if(sum < 0) left++;
else{
output.add(Arrays.asList(nums[i], nums[left], nums[right]));
while(right > left && nums[right] == nums[right - 1]) right--;
while(right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
return output;
}
}
Leetcode: 18 四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int len = nums.length;
List<List<Integer>> result = new ArrayList<>();
if(nums[0] > target / 4 || nums[len - 1] < target / 4) return result;
for (int i = 0; i < len && nums[i] <= target / 4 ; i++){
if(i > 0 && nums[i - 1] == nums[i]) continue;
for(int j = i + 1; j < nums.length && nums[i]+nums[j]<=target/2; j++){
if(j > i + 1 && nums[j - 1] == nums[j]) continue;
int left = j + 1;
int right = len - 1;
while(right > left){
int sum = nums[i] + nums[j] + nums[right] + nums[left];
if(sum > target) right--;
else if(sum < target) left++;
else{
result.add(Arrays.asList(nums[i], nums[j], nums[left],nums[right]));
while(left < right && nums[right] == nums[right-1]) right--;
while(left < right && nums[left] == nums[left+1]) left++;
right--;
left++;
}
}
}
}
return result;
}
}