454.四数相加II
1、代码(BC)
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map = new HashMap<>();
int count = 0;
for(int i : nums1){
for(int j : nums2){
int temp = i + j;
map.put(temp,++count);
}
}
int count1 = count;
for(int i : nums3){
for(int j : nums4){
int temp = 0 - (i + j);
if(map.containsKey(temp)){
count--;
}
}
}
return count1 - count;
}
}
2、代码(AC)
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map = new HashMap<>();
int temp ;
for(int i : nums1){
for(int j : nums2){
temp = i + j;
if(map.containsKey(temp)){
map.put(temp,map.get(temp) + 1 );
}else{
map.put(temp,1);
}
}
}
int count = 0;
for(int i : nums3){
for(int j : nums4){
temp = 0 - (i + j);
if(map.containsKey(temp)){
count += map.get(temp);
}
}
}
return count;
}
}
383. 赎金信
1、代码(AC)
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int flag;
int[] array = new int[26];
for(int i=0; i<magazine.length(); i++){
flag = magazine.charAt(i) - 'a';
array[flag]++;
}
for(int i=0; i<ransomNote.length(); i++){
flag = ransomNote.charAt(i) - 'a';
array[flag]--;
}
for(int count : array){
if(count < 0){
return false;
}
}
return true;
}
}
2、代码2(AC)
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record = new int[26];
for(char c : magazine.toCharArray()){
record[c - 'a'] += 1;
}
for(char c : ransomNote.toCharArray()){
record[c - 'a'] -= 1;
}
for(int i : record){
if(i < 0){
return false;
}
}
return true;
}
}
2、分析
(1)类似于有效异位词,通过哈希的思想将下标与字母相联系,将值与次数相联系。
15. 三数之和
1、代码(BC)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<Integer> record = new List<>();
int fast = 2;
int mid = 1;
int slow = 0;
while(slow < nums.length - 2){
while(mid < nums.length - 1){
while(fast < nums.length ){
if(nums[slow] + nums[mid] + nums[fast] == 1){
record[0][0] = nums[slow];
record[0][1] = nums[mid];
record[0][2] = nums[fast];
//如何赋值?如何使用指针?
}else{
fast++;
}
}
mid++;
fast = mid + 1;
}
slow++;
mid = slow + 1;
fast = mid + 1;
}
return record;
}
}
2、代码(AC)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int i=0; i<nums.length; i++){
//根据题意剪枝操作
//因为这是升序数组
if(nums[i] > 0){
return result;
}
//去重a -> 答案中不可以包含"数值"重复的三元组
if(i>0 && nums[i] == nums[i-1]){
continue;
}
int left = i +1;
int right = nums.length - 1;
while(left < right){
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]));
left++;
right--;
while(left<right && nums[left] == nums[left - 1]){
left++;
}
while(left<right && nums[right] == nums[right + 1]){
right--;
}
}
}
}
return result;
}
}
2、分析
(1)题目使用相向双指针进行遍历,由于题干说答案中不可包含重复的三元组,所以在遍历时也需要进行去重操作。
(2)双指针遍历条件
while(left < right)
18. 四数之和
1、代码
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; i++){
//剪枝操作,节省时间
// 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(left < right){
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]));
left++;
right--;
while(left < right && nums[left] == nums[left - 1]){
left++;
}
while(left < right && nums[right] == nums[right + 1]){
right--;
}
}
}
}
}
return result;
}
}
2、分析
(1)题目注意点说到:若两个四元组元素一一对应,则认为两个四元组重复。它指的是元素而非下标(例如[2,2,2,2,2]的情况),并不用对齐下标,所以需要对每个指针进行去重。
(2)剪枝操作从目前情况来看,想得到就写。
(3)ArrayList。