当前位置:网站首页>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
边栏推荐
- Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
- SuperMap game engine license module division
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- 一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
- DIY system home page, your personalized needs PRO system to meet!
- Interface control telerik UI for WPF - how to use radspreadsheet to record or comment
- 用C语言开发NES游戏(CC65)03、VRAM缓冲区
- 开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
- 图书馆自动预约脚本
- [apue] 文件中的空洞
猜你喜欢

Introduction to resttemplate

Yan Ji lost Beijing again, and more than half of the stores in the country were closed

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

Initialization examples of several modes of mma8452q

归并排序

Analysis of new retail e-commerce o2o model

VS code更新后不在原来位置

微创电生理通过注册:年营收1.9亿 微创批量生产上市企业

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

Developing NES games with C language (cc65) 10. Game cycle
随机推荐
Newly released, the domestic ide developed by Alibaba is completely open source
FlexPro软件:生产、研究和开发中的测量数据分析
05 pyechars 基本图表(示例代码+效果图)
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
C语言项目中使用json
STM32 loopback structure receives and processes serial port data
遭受痛苦和创伤后的四种本真姿态 齐泽克
Developing NES games with C language (cc65) 02. What is v-blank?
STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
快速读入
Why do enterprises need the ability of enterprise knowledge management?
04 pyechars 地理图表(示例代码+效果图)
sqli-labs(less-8)
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
SuperMap itablet license module division
Unity 安装 Device Simulator
AI制药的数据之困,分子建模能解吗?
Introduction to resttemplate
Custom paging tag 02 of JSP custom tag
Library automatic reservation script