leetcode 日记 3sumclosest java

整体思路为将threeSum将为twoSum即可

public int solution(int[] nums, int target) {
        if (nums.length == 3) {
            return nums[0] + nums[1] + nums[2];
        } else {
            Arrays.sort(nums);
            int r = 10000;//此两处10000为省事而设,如果严谨应该大致找到其中的一个较大距离
            int distance = 10000;
            for (int i = 0; i < nums.length; i++) {
                int[] temarray = new int[nums.length - 1];
                System.arraycopy(nums, 0, temarray, 0, i);
                System.arraycopy(nums, i + 1, temarray, i, nums.length - i - 1);
                int tem = twoSumClosest(temarray, target - nums[i]) + nums[i];
                int temdistance = tem - target;
                if (temdistance < 0) {
                    temdistance = -temdistance;
                } else if (temdistance == 0) {
                    return tem;
                }
                if (temdistance < distance) {
                    r = tem;
                    distance = temdistance;
                }
            }
            return r;
        }
    }

    private int twoSumClosest(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        int min = nums[r] + nums[l];
        int distance;
        if (min - target > 0) {
            distance = min - target;
        } else {
            distance = target - min;
        }
        while (l < r) {
            if (nums[l] + nums[r] == target)
                return nums[l] + nums[r];
            if (nums[l] + nums[r] < target) {
                if (target - (nums[l] + nums[r]) < distance) {
                    min = nums[l] + nums[r];
                    distance = target - (nums[l] + nums[r]);
                }
                l++;
                continue;
            }
            if (nums[l] + nums[r] > target) {
                if ((nums[l] + nums[r]) - target < distance) {
                    min = nums[l] + nums[r];
                    distance = (nums[l] + nums[r]) - target;
                }
                r--;
                continue;
            }
        }
        return min;
    }
时间: 2024-10-19 09:59:10

leetcode 日记 3sumclosest java的相关文章

leetcode 日记 4sum java

整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { List<List<Integer>> result = new ArrayList<>(); if((nums.length<4)||(nums==null)) { return result; } Arrays.sort(nums); if ((4*nums[0]&

Leetcode: Wildcard Matching. java

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

Leetcode: Text Justification. java

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad ex

leetcode 日记 162. Find Peak Element java python

根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个峰值,那么,每次对半分,便可以达到logn的复杂度. 根据对半分的思路继续想,不难发现只要确定了中间位置的数是处在上升阶段还是下降阶段,就可以确定在某一侧必有一个峰值. 往复多次,即可找出两个点,其中一个一定处于某一个峰值上. java代码: 1 public int findPeakElement

python leetcode 日记 --Contains Duplicate II --219

题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k. 给定一个整形数组和一个整数型数k,找出在这个数组中是否存在两个相同的数,并且这两个数的下标的距离小于k. "&q

【LeetCode刷题Java版】Evaluate Reverse Polish Notation(计算逆波兰表达式)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

LeetCode 有效数独 JAVA

思路很简单:   分别判断 行 列 box  是否有重复的数字,如果有重复的,返回 false . 单独拿一行来看. 1. 设置一个 HashMap<value, count> 2. put( value,  getOrDefault(value,0) + 1 ). 这里不用 get ( value ), 因为 java 中的 get 是通过 equals 实现的,泛型编程需要重写 equals 函数,才能正常使用 get . 而 getOrDefault(value, 0) 容器中有 val

LeetCode 链表题 ( Java )

leetcode 237. 删除链表中的节点 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 这道题比较简单,修改之前节点的 next 指针,使其指向之后的节点: /** * Definition for sin

LeetCode Plus One Java版解题报告

https://oj.leetcode.com/problems/plus-one/ 题意:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8; 解题思路,从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(n.length + 1)的数组,拷贝原来的数组. public class Solution { public int[] plusOne