当前位置:网站首页>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
边栏推荐
- STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
- 西门子对接Leuze BPS_304i 笔记
- First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
- Knowledge points of MySQL (13)
- AI制药的数据之困,分子建模能解吗?
- [apue] 文件中的空洞
- The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
- Distributed session solution
- 牛客网二叉树题解
- 行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
猜你喜欢

Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself

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

Introduction to resttemplate

LeetCode94. 二叉树的中序遍历

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

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

【萌新解题】爬楼梯

AVL tree (balanced search tree)
随机推荐
SuperMap iclient3d for webgl to realize floating thermal map
单调栈Monotonic Stack
Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
Uniapp 应用开机自启插件 Ba-Autoboot
Marketing play is changeable, and understanding the rules is the key!
Fastjson parses multi-level JSON strings
LeetCode206 反转链表
Functions and pointers in 08 go language
[cute new problem solving] climb stairs
区块反转(暑假每日一题 7)
05 pyechars 基本图表(示例代码+效果图)
新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间
VS1003调试例程
Using dependent packages to directly implement paging and SQL statements
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
Knowledge points of MySQL (13)
Markdown concise grammar manual
STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
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
leetcode:数组