LeetCode 454.四数相加II

分析1.0

这个最直接暴力法,不过过于暴力了,害怕.jpg

失误

读题理解失误:题目要求的是四元组的个数,读完题到我这里成了输出四元组,悲哉

分析2.0

记录有多少个可能为0的四元组就行了,两层for循环每次的下标索引都是不一样的,于是遍历前两组将元素和加入Map<Integer,Integer> <元素和,个数>,接着for循环遍历后两组,当 map.contains【0-它们的元素和】时,ans+map.get(对应个数);也可以使用getOrDefault()方法

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer,Integer> map = new HashMap();
        int sum1=0, sum2=0;
        int ans=0;
        // a+b+c+d=0
        for(int i=0; i<nums1.length; i++){
            for(int j=0; j<nums2.length; j++){
                sum1=0-(nums1[i]+nums2[j]);
                // 注意要+1
                map.put(sum1, map.getOrDefault(sum1, 0)+1);
            }
        }
        for(int i=0; i<nums3.length; i++){
            for(int j=0; j<nums4.length; j++){
                sum2=nums3[i]+nums4[j];
                ans+=map.getOrDefault(sum2,0);
            }
        }   
    return ans;             
    }
}

LeetCode 383. 赎金信

分析1.0

字符串src tar, tar中字符全部来自于src且src中对应字符数量>=src中对应字符数量 而且全部由小写英文字母组成,可采取数组形式的hash表解决问题

  1. 明确 字符-索引-元素值 关系
  2. 遍历src,将字符映射到数组对应的26个索引中
  3. 遍历tar,遇到一个字符,对应索引元素减一,若元素值<0 return
  4. return true
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] table = new int[26];
        for(int i=0; i<magazine.length(); i++){
            table[getPos(magazine.charAt(i))]++;
        }
        for(int i=0; i<ransomNote.length(); i++){
            int pos = getPos(ransomNote.charAt(i));
            table[pos]--;
            if(table[pos] < 0){
                return false;
            }
        }
        return true;
    }
    // 字符-索引对应关系
    int getPos(char c){
        return c-'a';
    }
}

LeetCode 15. 三数之和

分析1.0

给定一整数数组,要求返回多组索引,每组索引内的索引各不相同索引对应元素和为 0

暴力法三层for循环解决问题

失误 又看错题了,答案是要元素组合 不是索引组合 悲哉

分析2.0

抽象思维-已遍历部分-当前遍历元素-未遍历部分

for循环优化-前后双指针靠近优化遍历未遍历部分,之前遇到一个HashTable优化已遍历部分元素

class Solution {
    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; 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++;
                    // 上面去重后得到的依旧是result刚刚add进去的元素 所以要从下一个元素开始
                    right--; 
                    left++;
                }
            }
        }
        return result;
    }
}

LeetCode 18. 四数之和

分析1.0 

按照上题思路,排序+两层for循环+双指针

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++) {
		
            // 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;
    }
}

总结

  1. 考虑利用数学的等式关系,求①可以先求②再求①,深刻体现了辩证法的思想
  2. 判断条件想的不够全面,疏漏太多
  3. 数组考虑排序;for循环优化-前后双指针靠近优化遍历未遍历部分
  4. 注意边界条件

常用变量名增量更新

size、val、ans、cnt、cur、pre、next、left、right、index、gap、tar、res、src