当前位置:网站首页>Leetcode: array
Leetcode: array
2022-07-28 12:45:00 【Memorises1999】
Day2
The first question is :27 Remove elements 
Ideas :
- To know that the elements of an array are contiguous in memory address , You cannot delete an element in an array alone , Can only cover .
solution :
Solution 1 : Violence solution ( Two layers of for loop )
One for Loop through array elements , the second for Loop update array
// Time complexity :O(n^2)
// Spatial complexity :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) { // Find the elements that need to be removed , Just move the array forward one bit
for (int j = i + 1; j < size; j++) {
nums[j - 1] = nums[j];
}
i--; // Because the subscript i All the later values are moved forward by one bit , therefore i Also move forward one bit
size--; // The size of the array -1
}
}
return size;
}
};Solution 2 : Double pointer ( Through a fast pointer and a slow pointer in a for Loop through two for Cycle work )
Quick pointer : Find the elements of the new array
Slow pointer : Subscript to update the new array
// Time complexity :O(n)
// Spatial complexity :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;
}
}; The second question is :977 The square of an ordered array 
Ideas :
- Violence solution : First square , Sort according to the obtained data
- Double pointer :i Point to the starting position ,j Point to the end position ; Put the large values in the new array in reverse order
solution : Double pointer
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;) { // Pay attention here i <= j, Because there are two elements to deal with in the end
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;
}
};summary :
1、 Double pointer array and linked list operations are very common , A lot of research 、 Linked list 、 Interview questions for string and other operations , They all use double finger acupuncture
2、 Double finger needling can greatly reduce the time complexity , Improve the efficiency of the program
边栏推荐
- 区块反转(暑假每日一题 7)
- Communication example between upper computer and Mitsubishi fn2x
- 通过Jenkins 拉取服务器代码 权限不足问题及其他注意事项
- AI制药的数据之困,分子建模能解吗?
- MSP430 开发中遇到的坑(待续)
- 20220728-Object类常用方法
- [dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou
- 用C语言开发NES游戏(CC65)02、什么是v-blank?
- Unity加载Glb模型
- Kafaka丢消息吗
猜你喜欢

Interface control telerik UI for WPF - how to use radspreadsheet to record or comment

连通块&&食物链——(并查集小结)

C for循环内定义int i变量出现的重定义问题

揭秘界面控件DevExpress WinForms为何弃用受关注的MaskBox属性

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

HC-05蓝牙模块调试从模式和主模式经历

西门子对接Leuze BPS_304i 笔记

界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论

The usage and Simulation Implementation of vector in STL

Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
随机推荐
Kuzaobao: summary of Web3 encryption industry news on July 13
试用copilot过程中问题解决
FlexPro软件:生产、研究和开发中的测量数据分析
Redis implements distributed locks
SuperMap itablet license module division
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
Multiple items on a computer share a public-private key pair to pull the Gerrit server code
卸载 Navicat:正版 MySQL 官方客户端,真香!
Knowledge points of MySQL (13)
04 pyechars 地理图表(示例代码+效果图)
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
Some API interfaces purchased by Yiwu hope to bring you some help
Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
Why do enterprises need the ability of enterprise knowledge management?
【Base】优化性能到底在优化啥?
Use json.stringify() to format data
20220728-Object类常用方法
VS1003调试例程
leetcode:704二分查找
Functions and pointers in 08 go language