1. 四数相加II
思路:这道题采用了分组的思路,从而简化了解题的过程。
1.nums1,nums2为第一组,num3,num4为第二组。
2.将第一组中两个数组每个位置相加的每次结果做一个统计,并记录到hashmap中(key记录的是相加的值,value记录的是该值出现过的次数)。
3.将第二组中两个数组每个位置每次相加的结果取反,每得到一个取反值就去hashmap中查找是否有与其相等的值。设 int count = 0;用于统计结果,若查到则count += value。
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> recordMap = new HashMap<>();
int result = 0;
//循环遍历出num1,num2相加的每一种结果,并将结果和其出现的次数放到recordMap中
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
int temp = nums1[i] + nums2[j];
if (recordMap.containsKey(temp)) {
recordMap.put(temp, recordMap.get(temp) + 1);
} else {
recordMap.put(temp, 1);
}
}
}
//循环遍历出num3,num4相加的每一种结果,并将结果取反,去recordMap查找
for (int i = 0; i < nums3.length; i++) {
for (int j = 0; j < nums4.length; j++) {
int temp = nums3[i] + nums4[j];
//注意:result加上的是key对应的value
//若num1[0] + num2[0] = 5, num1[1] + num2[0] = 5 则key=5,对应的value是2,含义是结果相加出现5有两次
//那么当num3[0] + num4[0] = -5时,所需要记录的就是2,对应了两种不同的结果
if (recordMap.containsKey(-temp)) {
result += recordMap.get(-temp);
}
}
}
return result;
}
2. 赎金信
//暴力解法
public boolean canConstruct(String ransomNote, String magazine) {
char[] ints = new char[magazine.length()];
int count = 0;
for (int i = 0; i < magazine.length(); i++) {
ints[i] = magazine.charAt(i);
}
for (int i = 0; i < ransomNote.length(); i++) {
for (int j = 0; j < ints.length; j++) {
if (ransomNote.charAt(i) == ints[j]) {
ints[j] = ' ';
count++;
break;
}
}
}
return count == ransomNote.length();
}
//哈希解法
public boolean canConstruct02(String ransomNote, String magazine) {
//定义一个数组,用于记录各个字母出现的次数
int[] ints = new int[26];
//记录各个字母出现的次数
for (char c : magazine.toCharArray()) {
ints[c - 'a'] += 1;
}
//每需要一个用一个字母,所拥有的字母就少一个
for (char c : ransomNote.toCharArray()) {
ints[c - 'a'] -= 1;
}
//如果有一个位置的字母出现了负数则说明,magazine上的字母不够用
for (int anInt : ints) {
if (anInt < 0) {
return false;
}
}
return true;
}
3. 三数之和
跟着代码画图来理解
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
// 找出a + b + c = 0
// a = nums[i], b = nums[left], c = nums[right]
for (int i = 0; i < nums.length - 2; i++) {
// 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
if (nums[i] > 0) {
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) { // 去重a
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
// 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
while (right > left && nums[right] == nums[right - 1]){
right--;
}
while (right > left && nums[left] == nums[left + 1]){
left++;
}
right--;
left++;
}
}
}
return result;
}
4. 四数之和
跟着代码画图来理解
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i - 1] == nums[i]) { // 对nums[i]去重
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
// nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
// 对nums[left]和nums[right]去重
while (right > left && nums[right] == nums[right - 1]) {
right--;
}
while (right > left && nums[left] == nums[left + 1]) {
left++;
}
left++;
right--;
}
}
}
}
return result;
}