File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed
剑指 Offer II 006. 排序数组中两个数字之和
剑指 Offer II 008. 和大于等于 target 的最短子数组
剑指 Offer II 009. 乘积小于 K 的子数组 Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -9,4 +9,25 @@ class Solution {
9
9
}
10
10
}
11
11
}
12
+ };
13
+
14
+
15
+ // leetcode题目,下标不同
16
+ class Solution {
17
+ public:
18
+ vector<int > twoSum (vector<int >& numbers, int target) {
19
+ vector<int > ans;
20
+ for (int i = 0 ; i < numbers.size (); i++)
21
+ {
22
+ int x = target - numbers[i];
23
+ int j = lower_bound (numbers.begin () + i + 1 , numbers.end (), x) - numbers.begin ();
24
+ if ( j < numbers.size () && numbers[j] == x)
25
+ {
26
+ ans.push_back (i + 1 );
27
+ ans.push_back (j + 1 );
28
+ break ;
29
+ }
30
+ }
31
+ return ans;
32
+ }
12
33
};
Original file line number Diff line number Diff line change @@ -14,4 +14,21 @@ class Solution {
14
14
}
15
15
return ans == inf ? 0 : ans;
16
16
}
17
+ };
18
+
19
+ class Solution {
20
+ public:
21
+ int minSubArrayLen (int target, vector<int >& nums) {
22
+ int length = INT_MAX, sum = 0 ;
23
+ for (int fast = 0 , slow = 0 ; fast < nums.size (); fast++)
24
+ {
25
+ sum += nums[fast];
26
+ while (sum >= target)
27
+ {
28
+ length = min (length, fast - slow + 1 );
29
+ sum -= nums[slow++];
30
+ }
31
+ }
32
+ return length == INT_MAX ? 0 : length;
33
+ }
17
34
};
Original file line number Diff line number Diff line change @@ -12,4 +12,24 @@ class Solution {
12
12
}
13
13
return ans;
14
14
}
15
- };
15
+ };
16
+
17
+ class Solution {
18
+ public:
19
+ int numSubarrayProductLessThanK (vector<int >& nums, int k) {
20
+ int ans = 0 , product = 1 ;
21
+ for (int left = 0 , right = 0 ; right < nums.size (); right++)
22
+ {
23
+ product *= nums[right];
24
+ while (left <= right && product >= k)
25
+ {
26
+ product /= nums[left++];
27
+ }
28
+ if (left <= right)
29
+ {
30
+ ans += (right - left + 1 );
31
+ }
32
+ }
33
+ return ans;
34
+ }
35
+ };
You can’t perform that action at this time.
0 commit comments