代码随想录Day07

lc454

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
  Map<Integer,Integer> abmap=new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if (!abmap.containsKey(nums1[i]+nums2[j])){
                    abmap.put(nums1[i]+nums2[j],1);
                }else{
                    Integer integer = abmap.get(nums1[i] + nums2[j]);
                    integer++;
                    abmap.put(nums1[i] + nums2[j],integer);
                }
            }
        }
        int count=0;
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                if (abmap.containsKey(-nums3[i] - nums4[j])){
                    count+=abmap.get(-nums3[i] - nums4[j]);
                }
            }
        }

        return count;
    }
}

lc383

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
 
     if (magazine.length() < ransomNote.length()) {
        return false;
    }

    Map<Character, Integer> map = new HashMap<>();
    for (int i = 0; i < ransomNote.length(); i++) {
        char ran = ransomNote.charAt(i);
        map.put(ran, map.getOrDefault(ran, 0) + 1);
    }

    for (int i = 0; i < magazine.length(); i++) {
        char c = magazine.charAt(i);
        if (map.containsKey(c)) {
            if (map.get(c) <= 0) {
                continue;
            } else {
                map.put(c, map.get(c) - 1);
            }
        }
    }

    for (int count : map.values()) {
        if (count > 0) {
            return false;
        }
    }

    return true;
    }
}

lc15

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
  List<List<Integer>> list=new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>0){
                return list;
            }

            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{
                    list.add(Arrays.asList(nums[i],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 list;
    }
}

lc18

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;
    }
}

全部评论

相关推荐

10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
冷艳的小师弟在看机会:jd测评乱点直接被挂了,哭死~
点赞 评论 收藏
分享
10-16 14:02
南京大学 Java
淘天转正给我挂了,意料之中,但还是很伤心,我很怀疑自己&nbsp;你很优秀,可惜我们今年最不缺的就是优秀的人&nbsp;我是呢种很容易自我伤害的人,如果我的朋友背叛了我,我第一反应不是愤怒,而是自责,责备自己认人不清&nbsp;陌生的城市很大,大到没有容纳自己的地方&nbsp;出门的路口拐过去也不知道会到哪里去&nbsp;这一切就像一个巨大的他妈的袋鼠,狠狠的给了我一拳&nbsp;我觉得这样不好,我这样的人不适合呆在互联网,我的一个同学就很适合,我原以为她不适合的&nbsp;有一天面试,面试官问我,如果明天项目就要上线了,今天出现了一个重大严重bug,加班也解决不了,你怎么办?&nbsp;我叹了口气,想了想说,准备一下简历,晚上加班投一波&nbsp;最近秋招运气很差,和...
榨出爱国基因:很简单,我进厂不就是了,说完,他的气息不再掩饰,显露而出,再回流水线,竟是大专巅峰修为!一瞬间,流水线再次一寂,只见他挥手间就飞出三只蛊虫,一转苦力蛊,二转牛马蛊,三转吗喽蛊! 有趣,真有趣,精彩,实在是精彩。 我乃大专巅峰!谁敢叼我 谁能叼我! 又听他低吟: 电子厂中寒风吹 流水线上大神归 无休倒班万人退,本科悔而我不悔! 夜深, 他牢牢占据工位,转身低眉道:不过是些许夜班罢了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务