题目地址:https://leetcode-cn.com/problems/two-sum-less-than-k/

题目描述

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1.

Example 1:

  1. Input: A = [34,23,1,24,75,33,54,8], K = 60
  2. Output: 58
  3. Explanation:
  4. We can use 34 and 24 to sum 58 which is less than 60.

Example 2:

  1. Input: A = [10,20,30], K = 15
  2. Output: -1
  3. Explanation:
  4. In this case it's not possible to get a pair sum less that 15.

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i] <= 1000
  3. 1 <= K <= 2000

题目大意

给你一个整数数组 A 和一个整数 K,请在该数组中找出两个元素,使它们的和小于 K 但尽可能地接近 K,返回这两个元素的和。

如不存在这样的两个元素,请返回 -1。

解题方法

暴力求解

本来觉得这个题好难,但是看了数组的范围才有100个元素,果断暴力求解即可。

C++代码如下:

  1. class Solution {
  2. public:
  3. int twoSumLessThanK(vector<int>& A, int K) {
  4. const int N = A.size();
  5. int res = -1;
  6. int minDist = INT_MAX;
  7. for (int i = 0; i < N; i ++) {
  8. for (int j = i + 1; j < N; j ++) {
  9. int sum = A[i] + A[j];
  10. if (sum < K && K - sum < minDist) {
  11. minDist = K - sum;
  12. res = sum;
  13. }
  14. }
  15. }
  16. return res;
  17. }
  18. };

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】1099. Two Sum Less Than K 解题报告(C++)的更多相关文章

  1. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  2. LeetCode 1099. Two Sum Less Than K

    原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...

  3. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  4. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  5. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  6. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  7. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. 金蝶EAS——客户端打开时,提示正在更新的文件d:\eas\client\bin\lib\proxy.jar被其他应用程序占用.请关闭

    解决办法: 一.通过调用任务管理器来退出,启用任务管理器需同时按下键Ctrl+Alt+Del,在应用程序中找到金蝶EAS,单击,选择结束任务即可:或者在任务管理器中选择"进程",点 ...

  2. Shell 变量嵌套

    实现:eval 1 a="indv1" 2 indv1="Sus1" 3 4 eval tmp='$'$a 5 echo $tmp //这里 echo 返回值为 ...

  3. char*,string,char a[], const char *,之间的转换

    1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可.      EX: const char* tmp = "tsinghu ...

  4. 日常Java 2021/9/27

    题目: 在某个比赛中,有6个评委为参赛的选手打分,分数为1-100的随机整数.选手的最后得分为:除去最高分和最低分后的4个评委分值的平均值(不考虑小数部分). package m; import ja ...

  5. IT四大名著

    标题耸人听闻,sorry. CPU.操作系统.编译器和数据库我都不会.我英语也不行,但我认识所有的字母.:-) 万一有人感兴趣呢?https://sqlite.org/doclist.htmlThe ...

  6. abundant

    In ecology [生态学], local abundance is the relative representation of a species in a particular ecosys ...

  7. day05 django框架之路由层

    day05 django框架之路由层 今日内容概要 简易版django请求声明周期流程图(重要) 路由匹配 无名有名分组 反向解析 无名有名解析 路由分发 名称空间 伪静态 虚拟环境 简易版djang ...

  8. 启动spark-shell --master yarn的bug

    报错如下 18/06/06 15:55:31 ERROR cluster.YarnClientSchedulerBackend: Yarn application has already exited ...

  9. Scala(四)【集合基础入门】

    目录 一.Array 二. List 三.Set 四.Tuple 五.Map 一.Array package com.bigdata.scala.day01 /** * @description: 不 ...

  10. vue2 页面路由

    vue官方文档 src/views/Login.vue <template> <div> <h2>登录页</h2> </div> </ ...