LeetCode算法入门- 3Sum Closest -day10

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

解法思路:记得利用左边和右边两个指针进行遍历,会更快一些

代码如下:

class Solution {public int threeSumClosest(int[] nums, int target) {int len = nums.length;int min = Integer.MAX_VALUE;int result = 0;//记得先排好序Arrays.sort(nums);for(int i = 0; i < len; i++){//利用left和right两个指针int left = i + 1;int right = len - 1;while(left < right){int sum = nums[i] + nums[left] + nums[right];int diff = Math.abs(sum - target);if(diff < min){//用一个min值来保存差距最小的值,记得保存住此时的3sum值是多少min = diff;result = sum;}if(sum >= target)right--;elseleft++;}}return result;}
}

LeetCode算法入门- 3Sum Closest -day10相关推荐

  1. LeetCode算法入门- 3Sum -day9

    LeetCode算法入门- 3Sum -day9 题目描述: Given an array nums of n integers, are there elements a, b, c in nums ...

  2. LeetCode算法入门- Implement strStr() -day22

    LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...

  3. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  4. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  5. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  6. LeetCode算法入门- Multiply Strings -day18

    LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...

  7. LeetCode算法入门- Remove Nth Node From End of List -day17

    LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...

  8. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  9. LeetCode算法入门- Merge Two Sorted Lists -day15

    LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...

最新文章

  1. Sql Server2005 Transact-SQL 新兵器学习总结之-数据类型
  2. [WebApp]定宽网页设计下,固定宽度布局开发WebApp并实现多终端下WebApp布局自适应...
  3. python中类方法与实例方法的区别-python中类方法,实例方法,静态方法的作用和区别...
  4. 6.2 二叉树的定义、性质与存储结构
  5. mysql 在线备份脚本,MySQL备份脚本
  6. diskData磁盘数据分析
  7. idea中未被识别的maven项目,如何手动添加
  8. Luogu3403跳楼机
  9. html5鼠标悬停遮罩,js实现鼠标移动到图片产生遮罩效果
  10. 再谈mysql数据库之索引,联合索引,覆盖索引
  11. 调用MYSQL存储过程实例
  12. 定积分之几种常见曲线
  13. java代码到opcode_OPcode简介
  14. 计算机储存容量单位的进率,进制、存储与容量
  15. RestTemplate使用实战-exchange方法讲解-HTTP请求
  16. Citrix ADC 13.0 下载 百度网盘 按您的方式进行应用交付
  17. 今日头条2017校招笔试题
  18. jnz和djnz_djnz(单片机djnz指令的功能)
  19. 文献阅读——How to give an Academic Talk
  20. k图着色 遗传算法的简单python伪代码

热门文章

  1. 设计模式常见面试题汇总
  2. 一个多月的时间,终于把这件事做完了!
  3. 简单的小工具wordlight——让VS变量高亮起来
  4. 水印生成器第2版[原图质量水印可自定义设置]
  5. 火狐最实用的几款插件介绍[含附件]
  6. Typora颠覆写作体验的极简好用 Markdown 编辑器基本设置教程
  7. Spark集群完全分布式安装部署
  8. 图形驱动程序和显卡驱动什么区别_以后你的手机也需要单独安装显卡驱动程序了...
  9. python函数式编程读取数据时出现错误_Python编程中,函数遇到问题是抛出错误好还是约定返回值好?...
  10. 查看 rabbitmq 启动websocket 提示404_RabbitMQ 部署记录