-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2824.统计和小于目标的下标对数目.cpp
77 lines (77 loc) · 2.16 KB
/
2824.统计和小于目标的下标对数目.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode.cn id=2824 lang=cpp
*
* [2824] 统计和小于目标的下标对数目
*
* https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/description/
*
* algorithms
* Easy (85.67%)
* Likes: 19
* Dislikes: 0
* Total Accepted: 8.8K
* Total Submissions: 10.2K
* Testcase Example: '[-1,1,2,3,1]\n2'
*
* 给你一个下标从 0 开始长度为 n 的整数数组 nums 和一个整数 target ,请你返回满足 0 <= i < j < n 且 nums[i] +
* nums[j] < target 的下标对 (i, j) 的数目。
*
*
* 示例 1:
*
*
* 输入:nums = [-1,1,2,3,1], target = 2
* 输出:3
* 解释:总共有 3 个下标对满足题目描述:
* - (0, 1) ,0 < 1 且 nums[0] + nums[1] = 0 < target
* - (0, 2) ,0 < 2 且 nums[0] + nums[2] = 1 < target
* - (0, 4) ,0 < 4 且 nums[0] + nums[4] = 0 < target
* 注意 (0, 3) 不计入答案因为 nums[0] + nums[3] 不是严格小于 target 。
*
*
* 示例 2:
*
*
* 输入:nums = [-6,2,5,-2,-7,-1,3], target = -2
* 输出:10
* 解释:总共有 10 个下标对满足题目描述:
* - (0, 1) ,0 < 1 且 nums[0] + nums[1] = -4 < target
* - (0, 3) ,0 < 3 且 nums[0] + nums[3] = -8 < target
* - (0, 4) ,0 < 4 且 nums[0] + nums[4] = -13 < target
* - (0, 5) ,0 < 5 且 nums[0] + nums[5] = -7 < target
* - (0, 6) ,0 < 6 且 nums[0] + nums[6] = -3 < target
* - (1, 4) ,1 < 4 且 nums[1] + nums[4] = -5 < target
* - (3, 4) ,3 < 4 且 nums[3] + nums[4] = -9 < target
* - (3, 5) ,3 < 5 且 nums[3] + nums[5] = -3 < target
* - (4, 5) ,4 < 5 且 nums[4] + nums[5] = -8 < target
* - (4, 6) ,4 < 6 且 nums[4] + nums[6] = -4 < target
*
*
*
*
* 提示:
*
*
* 1 <= nums.length == n <= 50
* -50 <= nums[i], target <= 50
*
*
*/
#include <bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution {
public:
int countPairs(vector<int> &nums, int target) {
int ans = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[i] + nums[j] < target) {
ans++;
}
}
}
return ans;
}
};
// @lc code=end