2018.9.30 LeetCode 刷题日记 第16题

第 16 题  最接近目标数的三数之和

对一个数组来说,找出其中的三个数,使得三数之和与target最接近,最先想到的是暴力法求解,对i = 0; j = i+ 1; k = j+1;进行三重遍历,记录对target距离的最小值,但是三重循环,时间复杂度0(n3)。

改进 : 对寻求目标数来说,三数之和要么比target 大 ,要么比target小,确定寻找方向很重要,可以对数组进行先排序,再找寻正确组合

首先,排序好的数组可以从两侧向中间逼近,最小数和最大数加和,如果和比target大,可以从排序右侧向中间调节,反之从排序向左侧调节,同时注意保证左侧索引值比右侧小,其中若找到和target相等的三数之和,则可以直接返回,因为此时距离为0.

class Solution {
  public int threeSumClosest(int[] nums, int target) {
    int min = Integer.MAX_VALUE;
    int dis = 0;
    Arrays.sort(nums);
    int i = 0;
    int j = 0;
    int k = 0;
    for( i = 0; i < nums.length-2; i++){
      for(j = i+1, k = nums.length-1; j < k; ){
        dis =(nums[i] + nums[j] + nums[k]);
        if(Math.abs(dis-target) < Math.abs(min)){
        min = dis - target;
       }
      if(j < k && dis < target){
          j++;
      }else if(j < k && dis > target){
          k--;
      } else return target;
    }
  }
    return min+target;
  }
}

原文地址:https://www.cnblogs.com/tiansiyuan-program/p/9728889.html

时间: 2024-07-29 17:13:28

2018.9.30 LeetCode 刷题日记 第16题的相关文章

2018.10.02 LeetCode 刷题日记 第17题

手机小键盘 2-9 数字键上有分别对应的字母,输入一串数字,如234,则 2-abc,3-def,4-ghi,按顺序每个数字选择一个字母,输出全部的字母组合 从第一个数的第一个字母开始,向下找数,每种情况结束后进行回溯 读入 23 2- abc 选a 读3 - def 选d .选 e .选f 此时再进行回溯方法,索引已经超过范围,第一次结束 2中选b  依次进行回溯 回溯方法总结: 确定函数结束条件 依次往下找,知道触碰结束,回到上层,重新开始 代码如下: class Solution { pu

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2