当前位置:网站首页>剑指offer专项突击版第3天
剑指offer专项突击版第3天
2022-07-20 05:39:00 【hys__handsome】
思路
枚举第一个数 i i i ,然后让 t a r g e t target target 为 − n u m s [ i ] -nums[i] −nums[i],最后通过双指针去找所有两个数和为 t a r g e t target target 的组合。时间复杂度 O ( n 2 ) O(n^2) O(n2) 。
疑问:如何保证不出现重复的组合?
组合去重的思想与47. 全排列 II一样
做法:先升序排序,如果第二个数和第三个数分别与前一个数相等且没有正在被使用,则当前这个数不能使用,若使用了则会出现重复的序列。反之可以使用。
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];
//双指针找出和为target的两个数所有不重复组合
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;
}
};
剑指 Offer II 008. 和大于等于 target 的最短子数组
思路
看到求连续子数组的长度,直觉->双指针,注意子数组两端即可。
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;
}
};
思路
看到题目要求的是连续的子数组的个数,直觉要用双指针做。
果不其然,只要注意连续子数组的两端即可。
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; //有疑问的话可以手算一下
product *= nums[j++];
} else {
product /= nums[i++];
}
}
return cnt;
}
};
边栏推荐
- 回到原点:重新开始学习
- UGUI——EventSystems和射线检测
- RIoTBoard开发板系列笔记(六)—— buildroot构建系统镜像
- 聪明人的游戏提高篇:第三章第二课:因子个数(dcount)
- 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
- 学习笔记-浅谈MySql索引的数据结构与算法和索引的介绍
- Chapter005-FPGA学习之串口回传
- UGUI——RectMask2D
- Chapter002-FPGA学习之按键控制LED灯和蜂鸣器
- After xshell is installed, an error is reported when starting: mfc110 cannot be found DLL, unable to continue code execution. Reinstalling the program may fix this problem
猜你喜欢
Chapter005 serial port return of FPGA learning
Play with the one-stop plan of cann target detection and recognition [basic]
SourceTree对代码版本管理的操作流程及故障处理(不定时更新)
Title of graduation thesis on architectural decoration
JS构造二叉树
makefile中进行宏定义并传递给源代码
Stm32cubemx reads and writes USB flash disk through FatFs
UGUI——Graphic
Model thesis of radio, film and television program production
Model graduation thesis on traffic safety management
随机推荐
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
矿业工程毕业论文题目
JS构造二叉树
Pytorch realizes handwritten digit recognition | MNIST data set (fully connected neural network)
香蕉派BPI-M5折腾记录(3)—— 编译BSP
UGUI——Graphic
运算放大器的简要理解
HashCode详解
Small tips in spark SQL
Keras 载入历史.h5格式模型报错: AttributeError ‘str‘ object has no attribute ‘decode‘
学习记录十
回溯解决子集问题
unity 生命游戏简单实现
STM32CubeMX的CAN总线波特率设置
Title of graduation thesis on architectural decoration
pytorch实现手写数字识别 | MNIST数据集(全连接神经网络)
Optimizing graphics rendering in Unity games
SSM框架总和前的所有框架复习
cocos2dx 引擎中的一些技巧
Graduation thesis title of power system and automation [selected]