当前位置:网站首页>【***数组***】
【***数组***】
2022-06-23 06:21:00 【Micmic33】
数组理论基础
- 数组下标都是从0开始的。
- 数组内存空间的地址是连续的
C++中,二维数组的内存空间也是线性的连续的
二分查找
对于比较简单的二分查找来说,重点就是while里的条件和l,r指针的移动
第一种写法【l,r】内搜索
class Solution { public: int search(vector<int>& nums, int target) { int l=0; int r=nums.size()-1; while(l<=r){ int mid=l+(r-l)/2; if(nums[mid]==target) return mid; else if(nums[mid]<target){ l=mid+1; } else{ r=mid-1; } } return -1; } };第二种写法【l,r)内搜索
class Solution { public: int search(vector<int>& nums, int target) { int l=0; int r=nums.size(); while(l<r){ int mid=l+(r-l)/2; if(nums[mid]==target) return mid; else if(nums[mid]<target){ l=mid+1; } else{ r=mid; } } return -1; } };
移除元素

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slow=0,fast=0;
for(;fast<nums.size();fast++){
if(nums[fast]!=val)
nums[slow++]=nums[fast];
}
return slow;
}
};有序数组的平方
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int l=0,r=nums.size()-1;
int n=nums.size()-1;
vector<int> ans(nums.size(),0);
while(l<=r){
if(nums[l]*nums[l]>nums[r]*nums[r] ){
ans[n--]=nums[l]*nums[l];
l++;
}
else{
ans[n--]=nums[r]*nums[r];
r--;
}
}
return ans;
}
};长度最小的子数组
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int result = 100005;
int sum = 0; // 滑动窗口数值之和
int i = 0; // 滑动窗口起始位置
int Length = 0; // 滑动窗口的长度
for (int j = 0; j < nums.size(); j++) {
sum += nums[j];
// 注意这里使用while,每次更新 i(起始位置),并不断比较子序列是否符合条件
while (sum >= s) {
Length = (j - i + 1); // 取子序列的长度
result = min(result,Length);
sum -= nums[i++]; // 这里体现出滑动窗口的精髓之处,不断变更i(子序列的起始位置)
}
}
// 如果result没有被赋值的话,就返回0,说明没有符合条件的子序列
return result == 100005 ? 0 : result;
}
};边栏推荐
- Vs2013 ffmpeg environment configuration and common error handling
- A small method of debugging equipment serial port information with ADB
- 322. 零钱兑换
- 295. median data flow
- Open source oauth2 framework for SSO single sign on
- Idea automatically generates serialVersionUID
- 309. the best time to buy and sell stocks includes the freezing period
- asp. Net file download demo and related problems
- 406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)
- redux Actions may not have an undefined “type“ property. Have you misspelled a constant?
猜你喜欢

cmder

junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth

EndNote20使用教程分享(未完

【STL】pair用法总结

RFID数据安全性实验:C#可视化实现奇偶校验、CRC冗余校验、海明码校验
![[STL] summary of map usage of associated containers](/img/1d/1b6488ea47face0548500b1e1ec60d.png)
[STL] summary of map usage of associated containers

图解三次握手四次挥手,小白都能看懂
![[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed](/img/aa/0189beb065fa0d4b625390793cb79b.png)
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed

The illustration shows three handshakes and four waves. Xiaobai can understand them
![[STL] summary of deque usage of sequential containers](/img/33/65c54d14697ee43b2655ea1255d67d.png)
[STL] summary of deque usage of sequential containers
随机推荐
406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
897. incremental sequential search tree
【STL】关联容器之unordered_map用法总结
309. the best time to buy and sell stocks includes the freezing period
About Supervision
/Bin/sh no such file or directory problem
WPF command directive and inotifypropertychanged
asp. Net file download demo and related problems
MySQL redo log redo log
303. 区域和检索 - 数组不可变
Add IPAD control function into shairplay
316. 去除重复字母
redux Actions may not have an undefined “type“ property. Have you misspelled a constant?
The illustration shows three handshakes and four waves. Xiaobai can understand them
初始化层实现
Run typescript code directly using TS node
宝塔忘记密码
[STL] summary of deque usage of sequential containers
Traversal of binary tree and related knowledge
[project training 10] drawing of arrows

