454四数相加II
两两为一组计算和,建立哈希表存储第一二组的和,遍历三四组的和,在哈希表中查找。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> mapList = new HashMap<>();
for (int i: nums1) {
for (int j: nums2) {
if (mapList.containsKey(i+j)){
mapList.put(i+j, mapList.get(i+j)+1);
}else {
mapList.put(i+j, 1);
}
}
}
int count = 0;
for (int i: nums3) {
for (int j: nums4) {
if (mapList.containsKey(-i-j)){
count = count + mapList.get(-i-j);
}
}
}
return count;
}
}
383赎金信
建立容量为26的数组,用于标记26个英文字母,通过-'a'的操作计算字母对应索引。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] nums = new int[26];
for (char c:magazine.toCharArray()) {
nums[c-'a']++;
}
for (char c:ransomNote.toCharArray()) {
nums[c-'a']--;
if (nums[c-'a']<0){
return false;
}
}
return true;
}
}
15三数之和
返回所有和为 0
且不重复的三元组。
我的题解:排序+双指针
其中最难的是重复的三元组的处理,因此运用双指针,进行两数之和的计算和重复三元组的处理
设置i,l,r三个指针,其中l和r指针用于两数之和的计算。
第一步对重复三元组的处理是针对i的:如果与前一个元素相同则跳过
if (i>=1 && nums[i] == nums[i-1]){
continue;
}
第一步对重复三元组的处理是针对l和r的:
针对l,如果与前一个元素相同则跳过
针对r,如果与后一个元素相同则跳过
boolean flag = true;
if (l>i+1 && nums[l]==nums[l-1]){
l++;
flag = false;
}
if (r<nums.length-1 && nums[r]==nums[r+1]){
r--;
flag = false;
}
整体代码如下:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i]>0){
return result;
}
if (i>=1 && nums[i] == nums[i-1]){
continue;
}
int l = i+1;
int r = nums.length-1;
while (l<r){
if (nums[l]+nums[r]==-nums[i]){
boolean flag = true;
if (l>i+1 && nums[l]==nums[l-1]){
l++;
flag = false;
}
if (r<nums.length-1 && nums[r]==nums[r+1]){
r--;
flag = false;
}
if (flag){
List<Integer> list = Arrays.asList(nums[i], nums[r], nums[l]);
result.add(list);
l++;
r--;
}
}else if (nums[l]+nums[r]>-nums[i]){
r--;
}else {
l++;
}
}
}
return result;
}
}
18四数之和
返回和为target的不重复的四元组
遍历i,j找到头两个数,求和后的步骤,同三数之和。
class Solution {
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-3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
return result;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
break;
}
int l = j + 1;
int r = nums.length - 1;
while (l < r) {
if ((long)nums[l] + nums[r] == (long)target - nums[i] - nums[j]) {
boolean flag = true;
if (l > j + 1 && nums[l] == nums[l - 1]) {
l++;
flag = false;
}
if (r < nums.length - 1 && nums[r] == nums[r + 1]) {
r--;
flag = false;
}
if (flag) {
List<Integer> list = Arrays.asList(nums[i], nums[j], nums[r], nums[l]);
result.add(list);
l++;
r--;
}
} else if ((long)nums[l] + nums[r] > (long)target - nums[i] - nums[j]) {
r--;
} else {
l++;
}
}
}
}
return result;
}
}
收获
在只包含英文字母的字符串的操作时,可以使用int[26]来存储各个字母出现个数
在查找不重复的组合时,使用哈希表很难处理,应使用双指针用于重复处理。