首页 > 其他分享 >923打卡

923打卡

时间:2023-09-23 16:22:06浏览次数:35  
标签:right target nums int res 打卡 923 first

1. 三数之和 (15)

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
      List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int first = 0; first < nums.length; first++) {
            if(first>0 && nums[first] == nums[first-1])
                continue;
            int third = nums.length-1;
            int target = -nums[first];
            for (int second = first+1; second <third ; second++) {
                if(second>first+1 && nums[second] ==nums[second-1])
                    continue;
                while(second<third && nums[second]+nums[third] > target)
                    third--;
                if(second == third)
                    break;
                if ( nums[second]+nums[third]==target){
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    res.add(list);
                }
            }
        }
        return res;
    }
}

2. 最接近的三数之和(16)

给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。

class Solution {
    public int threeSumClosest(int[] nums, int target) {
  int best = Integer.MAX_VALUE;
  Arrays.sort(nums);
        for (int first = 0; first < nums.length ; first++) {
            if(first>0 && nums[first]==nums[first-1])
                continue;
            int second = first+1;
            int third = nums.length-1;


            while ( second <third) {

                int sum  = nums[first]+nums[second]+nums[third];
                if(sum ==target)
                    return sum;

                if( Math.abs(sum-target) <Math.abs(best-target)){
                    best = sum;
                }
                if (sum >target){
                    int right = third-1;
                    while (second<right && nums[right]== nums[third])
                        right--;
                    third =right;
                }else {
                    int left = second + 1;
                    while (left < third && nums[left] == nums[second])
                        left++;
                    second = left;
                }
            }
        }
            return best;
        }

}

3. 电话号码的组合(17)

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

 
class Solution {
     public List<String> letterCombinations(String digits) {
        ArrayList<String> res = new ArrayList<>();
        if (digits == null || digits.length() == 0)
            return res;
        StringBuffer combination = new StringBuffer();
        backstack(res, digits, 0, combination);
        return res;
    }

    HashMap<Character, String> map = new HashMap<Character, String>() {{
        put('2', "abc");
        put('3', "def");
        put('4', "ghi");
        put('5', "jkl");
        put('6', "mno");
        put('7', "pqrs");
        put('8', "tuv");
        put('9', "wxyz");

    }};

    private void backstack(ArrayList<String> res, String digits, int index, StringBuffer combination) {
        if (index == digits.length())
            res.add(combination.toString());
        else {
            String letters = map.get(digits.charAt(index));
            for (int i = 0; i < letters.length(); i++) {
                combination.append(letters.charAt(i));
                backstack(res, digits, index + 1, combination);
                combination.deleteCharAt(index);
            }
        }


    }
}

4. 四数之和(18)

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复)

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
     ArrayList<List<Integer>> list = new ArrayList<>();
        if (nums == null || nums.length < 4) return list;
        Arrays.sort(nums);
        for (int i = 0; i <= nums.length - 4; i++) {
            //剪枝
            if (nums[i] > 0 && nums[i] > target) {
                break;
            }
            //去重
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j <= nums.length - 3; j++) {
                //剪枝,考虑数组元素有负数
                if (nums[i] + nums[j] > target && target > 0) {
                    break;
                }
                //去重
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int left = j + 1;
                int right = nums.length - 1;
                while (left < right) {
                    if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
                        left++;
                    } else if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
                        right--;
                    } else {
                        ArrayList<Integer> res = new ArrayList<>();
                        res.add(nums[i]);
                        res.add(nums[j]);
                        res.add(nums[left]);
                        res.add(nums[right]);
                        list.add(res);
                        while (right > left && nums[right] == nums[right - 1])
                            right--;
                        while (right > left && nums[left] == nums[left + 1])
                            left++;
                        right--;
                        left++;
                    }
                }
            }
        }
        return list;

    }
}

 

标签:right,target,nums,int,res,打卡,923,first
From: https://www.cnblogs.com/forever-fate/p/17724469.html

相关文章

  • 20230923
    //assure,beneficial,correspond,courtesy,desirous,deteriorate,discussion,interim,keen,maintain,requirement,valid,regularcustomer,substantialbusinessassure-保证Toassuremeanstogivesomeoneconfidenceorcertaintyaboutsomething.Itinv......
  • 打卡
    9月22日:今天上午上了形势与政策课程,下午我们学习了高级英语,通过两节课的学习,我有从中学习了单词与语法,与此同时今天我也将java课的课后作业完成。明天做数据结构与算法的课后习题,然后抽时间学习一下java。......
  • 每日打卡 周五 九月二十二日
    今天又上英语课,快要四级考试了,得抓紧学习英语,下午完课后,看了一会儿英语单词,主要是翻译有问题,以前没有做过的题型也会一直是难,主要是想不词语的意思可以那样用。其实主要功课就是背单词,现在词汇积累的少,出现都不认识,在着就是听听力,真的是要很好的训练了。......
  • 922打卡
    1.盛最多水的容器(11)给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i,0) 和 (i,height[i]) 思想:双指针classSolution{publicintmaxArea(int[]height){intleft=0;intright=height.length-1;i......
  • 算法打卡|Day2 数组part02
    Day2数组part02今日任务:977.有序数组的平方,209.长度最小的子数组,59.螺旋矩阵II目录Day2数组part02今日任务:977.有序数组的平方,209.长度最小的子数组,59.螺旋矩阵IIProblem:977.有序数组的平方思路解题方法复杂度CodeProblem:209.长度最小的子数组思路解题方法复杂......
  • 921打卡
    1.N字形变换(06)将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z字形排列。比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:PAHNAPLSIIGYIR之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNA......
  • 每日打卡 周三 九月二十日
    今天就早上八点有一节课,完课后回到宿舍,最近有点感冒,大概10点到宿舍,躺在床上就睡过去了,起来就该吃午饭。下午本应该学习javaweb但是一直没有状态,只看了一会就感到头晕,躺在床上又睡过去了,起来天已经黑了,简单对付几口,吃过药就没有再出宿舍了。......
  • 9.20打卡带哨兵的双向环形链表
      importjava.util.ArrayList;//双向环形链表哨兵既是头也是尾哨兵的prev指向最后一个元素,最后一个元素的next指向哨兵publicclassMain{publicstaticvoidmain(String[]args){DoubleLinkedListd=newDoubleLinkedList();d.addFirst(3);......
  • 大二打卡(9.20)
    今天做了什么:英语课,今天帮英语老师擦了黑板,被老师感谢,开心,上课时候好多高中笔记本上记过的词汇短语都记不住汉语意思了,又是想念高中笔记本的一天,然后上节课讲的短语也没记住,只记得写在哪个位置了,可悲,今天遇到什么问题:英语小测试做题速度大不如前,没有写完全部题目,好多生词不认识......
  • 920打卡
    1.两数相加(02)给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字0之外,这两个数都不会以0 开头。思想:遍历,考虑进位和链表......