当前位置:网站首页>leetcode:数组
leetcode:数组
2022-07-28 11:37:00 【Memorises1999】
Day2
第一题:27移除元素
思路:
- 要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。
解法:
解法一:暴力解法(两层for循环)
一个for循环遍历数组元素 ,第二个for循环更新数组
// 时间复杂度:O(n^2)
// 空间复杂度:O(1)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size = nums.size();
for (int i = 0; i < size; i++) {
if (nums[i] == val) { // 发现需要移除的元素,就将数组集体向前移动一位
for (int j = i + 1; j < size; j++) {
nums[j - 1] = nums[j];
}
i--; // 因为下标i以后的数值都向前移动了一位,所以i也向前移动一位
size--; // 此时数组的大小-1
}
}
return size;
}
};解法二:双指针(通过一个快指针和慢指针在一个for循环下完成两个for循环的工作)
快指针:寻找新数组的元素
慢指针:指向更新新数组的下标
// 时间复杂度:O(n)
// 空间复杂度:O(1)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slow = 0;
for (int fast = 0; fast < nums.size(); fast++) {
if (val != nums[fast]) {
nums[slow++] = nums[fast];
}
}
return slow;
}
};第二题:977有序数组的平方
思路:
- 暴力解法:先平方,在根据得到的数据进行排序
- 双指针:i指向起始位置,j指向终止位置;将大的值逆序放入新数组中
解法:双指针
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
int k = A.size() - 1;
vector<int> result(A.size(), 0);
for (int i = 0, j = A.size() - 1; i <= j;) { // 注意这里要i <= j,因为最后要处理两个元素
if (A[i] * A[i] < A[j] * A[j]) {
result[k--] = A[j] * A[j];
j--;
}
else {
result[k--] = A[i] * A[i];
i++;
}
}
return result;
}
};总结:
1、双指针法数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法
2、双指针法能大大降低时间复杂度,提高程序的效率
边栏推荐
- 【vulnhub】Raven2
- 【vulnhub】Raven2
- Yan Ji lost Beijing again, and more than half of the stores in the country were closed
- 洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
- ViewPager2+Fragment
- 卸载 Navicat:正版 MySQL 官方客户端,真香!
- Developing NES games (cc65) 05 and palette with C language
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- 用C语言开发NES游戏(CC65)02、什么是v-blank?
- On Governance and innovation | the openanolis sub forum of the 2022 open atom global open source summit was successfully held
猜你喜欢

Zhou Hongyi talks about Internet thinking: users, not customers

The usage and Simulation Implementation of vector in STL

软件架构师必需要了解的 saas 架构设计?

IRBuilder

Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held

开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开

Tencent two sides: @bean and @component are used in the same class, what will happen?

Marketing play is changeable, and understanding the rules is the key!

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东

华为发布HarmonyOS 3及全场景新品,智慧体验更进一步
随机推荐
洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
Exploration on cache design optimization of community like business
8000 word explanation of OBSA principle and application practice
PHP date calculation operation processing, the current date plus one day and the specified date minus one day
Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
Force buckle 7_ 1672. Total assets of the richest customers
IRBuilder
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
社区点赞业务缓存设计优化探索
数字经济时代的开源数据库创新 | 2022 开放原子全球开源峰会数据库分论坛圆满召开
【vulnhub】Raven2
SQL注入 Less26(过滤空格和注释符,使用不带空格的报错注入)
PHP获取本周所有日期或者最近七天所有日期
AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!
Developing NES games with C language (cc65) 11. Metatiles
Zhou Hongyi talks about Internet thinking: users, not customers
Multi Chain and multi currency wallet system development cross chain technology
1331. 数组序号转换 : 简单模拟题
The usage and Simulation Implementation of vector in STL
【Try to Hack】AT、SC、PS命令提权