2021-01-14

打卡打卡

Java实现leetcode16、17、21题

16.最接近的三数之和

题目描述

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

示例:
输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

思路

先对数组进行排序,外层循环遍历a,内层双指针左右接近

代码
class Solution {
    
    
    public int threeSumClosest(int[] nums, int target) {
    
    
        Arrays.sort(nums);
        int len=nums.length;
        int best=1000000;
        for(int i=0;i<len;i++){
    
    //循环a
            if(i>0&&nums[i]==nums[i-1]){
    
    //避免相同元素
                continue;
            }
            int j=i+1,k=len-1;//双指针
            while(j<k){
    
    
                int sum=nums[i]+nums[j]+nums[k];
                if(sum==target){
    
    //相同即结束
                    return target;
                }
                if(Math.abs(sum-target)<Math.abs(best-target)){
    
    //缩小target和sum的值
                    best=sum;
                }
                //移动指针
                if(sum>target){
    
    
                    int k1=k-1;
                    while (j < k1 && nums[k] == nums[k1]) {
    
    
                        --k1;
                    }
                    k=k1;
                    // k--;
                    // while (j < k && nums[k] == nums[k-1]) {
    
    
                    //     --k;
                    // }
                }else{
    
    
                    // j++;
                    // while (j < k && nums[j] == nums[j+1]) {
    
    
                    //     ++j;
                    // }
                    int j1=j+1;
                    while (j1 < k && nums[j] == nums[j1]) {
    
    
                        ++j1;
                    }
                    j=j1;
                }
            }
        }
        return best;
    }
}

17.电话号码的字母组合

题目描述

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

在这里插入图片描述
示例:

输入:“23”
输出:[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

思路

哈希表存储,慢慢取出来

代码
class Solution {
    
    
    public List<String> letterCombinations(String digits) {
    
    
        List<String> combinations = new ArrayList<String>();
        if (digits.length() == 0) {
    
    
            return combinations;
        }
        Map<Character, String> phoneMap = 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");
        }};
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        return combinations;
    }

    public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
    
    
        if (index == digits.length()) {
    
    
            combinations.add(combination.toString());
        } else {
    
    
            char digit = digits.charAt(index);
            String letters = phoneMap.get(digit);
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
    
    
                combination.append(letters.charAt(i));
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                combination.deleteCharAt(index);
            }
        }
    }
}

21.合并两个有序链表

题目描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:
输入:l1 = [], l2 = []
输出:[]

示例 3:
输入:l1 = [], l2 = [0]
输出:[0]

提示:
两个链表的节点数目范围是 [0, 50]
-100 <= Node.val <= 100
l1 和 l2 均按 非递减顺序 排列

思路

迭代,学过数据结构的都会

代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
        ListNode prehead = new ListNode(-1);

        ListNode prev = prehead;
        while (l1 != null && l2 != null) {
    
    
            if (l1.val <= l2.val) {
    
    
                prev.next = l1;
                l1 = l1.next;
            } else {
    
    
                prev.next = l2;
                l2 = l2.next;
            }
            prev = prev.next;
        }
        prev.next = l1 == null ? l2 : l1;

        return prehead.next;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45847565/article/details/112631302