当前位置:网站首页>【***数组***】
【***数组***】
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;
}
};边栏推荐
猜你喜欢

深度学习系列46:人脸图像超分GFP-GAN

MySQL optimization
![Don't look for [12 super easy-to-use Google plug-ins are here] (are you sure you want to take a look?)](/img/45/3e43faf7aba6741825ccb9719b8445.png)
Don't look for [12 super easy-to-use Google plug-ins are here] (are you sure you want to take a look?)

Project_ Filter to solve Chinese garbled code

MySQL的意向共享锁、意向排它锁和死锁

The illustration shows three handshakes and four waves. Xiaobai can understand them

MySQL basic query

为什么TCP协议是三次握手而不是两次?

ssm + ftp +ueditor

Xiaobai must see in investment and wealth management: illustrated fund buying and selling rules
随机推荐
316. 去除重复字母
产品-Axure9(英文版),原型设计 制作下拉二级菜单
初始化层实现
redux Actions may not have an undefined “type“ property. Have you misspelled a constant?
别找了诸位 【十二款超级好用的谷歌插件都在这】(确定不来看看?)
Open source oauth2 framework for SSO single sign on
System permission program cannot access SD card
307. area and retrieval - array modifiable
306. 累加数
对二进制的某一位操作
OSI分层模型对工作的具体帮助
Concepts and differences of DQL, DML, DDL and DCL
407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
excel高级绘图技巧100讲(八)-Excel绘制WIFI图
About professional attitude
A small method of debugging equipment serial port information with ADB
Storage mode of data in memory (C language)
Why can't the index of JS array use negative numbers
407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
How to migrate virtual machines from VirtualBox to hype-v

