当前位置:网站首页>Sword finger offer special assault version day 3
Sword finger offer special assault version day 3
2022-07-21 04:01:00 【hys__ handsome】
The finger of the sword Offer II 007. Array and 0 Three numbers of
Ideas
Enumerate the first number i i i , And then let t a r g e t target target by − n u m s [ i ] -nums[i] −nums[i], Finally, find the sum of all two numbers through double pointers t a r g e t target target The combination of . Time complexity O ( n 2 ) O(n^2) O(n2) .
doubt : How to ensure that there are no duplicate combinations ?
Combine the idea of de duplication with 47. Full Permutation II equally
practice : Sort in ascending order first , If the second number and the third number are equal to the previous number respectively and are not being used , Then the current number cannot be used , If used, there will be repeated sequences . Instead, you can use .
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(),nums.end());
for(int i = 0; i < n-2; i++) {
if(i!=0 && nums[i] == nums[i-1]) continue;
int target = -nums[i];
// Double pointer finds and is target All non repeated combinations of the two numbers of
for(int j = i+1, k = n-1; j < k; j++) {
if(nums[j] == nums[j-1] && j-1 != i) continue;
if(nums[j] + nums[k] > target)
k--, j--;
else if(nums[j] + nums[k] == target)
res.push_back({
nums[i],nums[j],nums[k]});
}
}
return res;
}
};
The finger of the sword Offer II 008. And greater than or equal to target The shortest subarray of
Ideas
Seeing and seeking Continuous subarray The length of , intuition -> Double pointer , Pay attention to both ends of the subarray .
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int i = 0, j = 0, res = 0x3f3f3f3f;
int sum = 0;
while(i <= j && j < n){
if(sum + nums[j] < target) sum += nums[j++];
else res = min(res,j-i+1), sum -= nums[i++];
}
if(res == 0x3f3f3f3f) return 0;
else return res;
}
};
The finger of the sword Offer II 009. The product is less than K Subarray
Ideas
See what the title requires Number of consecutive subarrays , Intuition should be done with double pointers .
Sure enough , Just pay attention to both ends of the continuous subarray .
class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
int n = nums.size();
int i = 0, j = 0, cnt = 0;
int product = 1;
while(i <= j && j < n){
if(nums[j] * product < k) {
cnt += j-i+1; // If you have any questions, you can count them by hand
product *= nums[j++];
} else {
product /= nums[i++];
}
}
return cnt;
}
};
边栏推荐
- SourceTree对代码版本管理的操作流程及故障处理(不定时更新)
- Graduation thesis title of power system and automation [selected]
- 解决‘_xsrf’ argument missing from POST问题
- 把项目发布网上,不想开端口
- uni-app配置
- Given a positive integer n, it is expressed as the addition of numbers 1,3,7,15. Please code to find the combination that minimizes the total number of occurrences of the above numbers (each number ca
- 聪明人的游戏提高篇:第三章第一课:isbn (ISBN码)
- HDF5 library version mismatched error的解决方案
- 园林专业毕业论文范文
- 色彩空间(2)—— YUV
猜你喜欢
学习笔记-浅谈MySql索引的数据结构与算法和索引的介绍
[Unity脚本优化] Optimizing garbage collection in Unity games
Chapter006-FPGA学习之LCD显示
vsCode配置Eslint+Prettier结合使用详细配置步骤,规范化开发
TensorFlow的学习笔记(一)
Topic of graduation thesis on Urban Rail Transit Engineering
Chapter004-FPGA学习之IP核相关功能【时钟、RAM、FIFO】
学习记录十三
Chapter005 serial port return of FPGA learning
HashCode详解
随机推荐
unity 生命游戏简单实现
解读最新ECCV 2022工作:组合式扩散模型
Small tips in spark SQL
STM32CubeMonitor的使用第一部分-数据绘图以及仪表显示
STM32CubeMX的CAN总线波特率设置
时间格式化
STM32CubeMonitor的使用第二部分-历史数据存储以及网络访问
建筑工程测量与测绘毕业论文范文
建筑装饰毕业论文题目
UGUI——EventSystems和射线检测
HDF5 library version mismatched error的解决方案
石油工程毕业论文范文
输配电及用电工程毕业论文题目
JupyterNotebook插件管理与安装
Title of graduation thesis on Mining Engineering
并发编程day01
请问一下 flink1.15 flinksql kafka topic如果不提前创建提交作业就会失败
好玩的猜数游戏(不是二分查找!四位数)
学习记录五
园林专业毕业论文范文