当前位置:网站首页>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、双指针法能大大降低时间复杂度,提高程序的效率
边栏推荐
- 新零售电商O2O模式解析
- 用C语言开发NES游戏(CC65)09、滚动
- How can a novice quickly complete the establishment of a website? Come to the free "fitting room" experience
- PHP date time application: add or subtract the number of days of a specific date
- Several ways to bind controls --butterknife/viewbinding/databinding
- With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright
- 数字经济时代的开源数据库创新 | 2022 开放原子全球开源峰会数据库分论坛圆满召开
- 聚变云原生,赋能新里程 | 2022 开放原子全球开源峰会云原生分论坛圆满召开
- After abolishing Tencent cloud: meiyabaike won the bid of 98.3 million
- PHP日期时间运用:添加或减去特定日期的天数
猜你喜欢

DIY system home page, your personalized needs PRO system to meet!

金九银十 再不卷就来不及了

Basic use of JSON server

恋爱男女十禁

HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing

Knowledge points of MySQL (13)

Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing

要想组建敏捷团队,这些方法不可少

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

Kafaka丢消息吗
随机推荐
云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
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
Zhou Hongyi talks about Internet thinking: users, not customers
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
What if the instruction set umi2 is missing? PTK installation cannot be carried out
用C语言开发NES游戏(CC65)08、背景 碰撞
[try to hack] intranet Foundation
PHP日期时间运用:添加或减去特定日期的天数
Introduction to resttemplate
Google Earth engine (GEE) -- problems in the use of coordinate projection and reduceresolution functions in image downscaling
用C语言开发NES游戏(CC65)06、精灵
【vulnhub】Raven2
Developing NES games with C language (cc65) 11. Metatiles
Multi Chain and multi currency wallet system development cross chain technology
Implementation method of mouse hover, click and double click in ue4/5
ViewPager2+Fragment
laravel表单数据验证