当前位置:网站首页>Double pointer | 977 Square of ordered array
Double pointer | 977 Square of ordered array
2022-06-10 02:16:00 【lee2813】
One 、 subject

Two 、 Answer key
Double pointer
Left pointer : Traverse from left to right , Divide valid value boundary , At this time, the array of valid values is [0,left]
Right pointer : Traverse from right to left , Divide valid value boundary , At this time, the array of valid values is [right,nums.size()-1]
3、 ... and 、 Code
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int right = nums.size() - 1;
int left = 0;
vector<int> ans(nums.size());
int pos =nums.size() - 1;
while(left < right){
if(nums[left] * nums[left] > nums[right] * nums[right]){
ans[pos--] = nums[left] * nums[left];
left++;
}else{
ans[pos--] = nums[right] * nums[right];
right--;
}
}
ans[pos] = nums[left] * nums[left];
return ans;
}
};
边栏推荐
- 服务器停止响应是什么意思,该如何排查?
- Brief introduction to Allan variance definition and calculation method
- 2022年上海市安全员C证考试模拟100题及模拟考试
- How to solve the problem of incorrect IP comments after WordPress starts CDN
- (w10)Prtsc截图键无法正常使用——解决办法
- 起薪1800,三年来手头只积攒一万块,裸辞奔赴一线城市月薪达3万
- Wechat applet saves video to photo album wx saveVideoToPhotosAlbum()
- (W10) prtsc screenshot key cannot be used normally - solution
- Dry goods | touch screen operation automation of mobile app automation
- What are the challenges facing energy project management? Skills and tools for successfully managing energy projects
猜你喜欢

Can write a desktop digital hour clock program without programming, which can be easily realized in only 3 steps

LABVIEW_ Classroom notes random (XIII) sub VI

With all due respect: programmers spend most of their time not writing code, but...

Smart Cloud Light Gateway Service pour améliorer l'efficacité de la gestion de la production

What does the server stop responding mean and how to troubleshoot it?

Low code analysis and inventory: two misunderstandings need to be avoided in the application of low code in the banking industry

2022年广东省安全员A证第三批(主要负责人)考试模拟100题及答案

Phantom engine plug-in - Maya LIVELINK - install and use

Cordova website packaging_ Cordova packaging website

35岁职场焦虑,越来越多的人这样选择
随机推荐
Allan方差定義與計算方法簡介
(w10)Prtsc截图键无法正常使用——解决办法
code....
Allan方差定义与计算方法简介
Flutter series: foundation of material theme - materialapp
Unity Netcode for GameObject 1.0.0 学习笔记(一)—— Hello World Demo
MySQL installation + test connection + common terminal MySQL commands (super detailed)
不会编程也能亲手编写一个桌面数字小时钟程序,仅3步轻松实现
10个常见触发IO瓶颈的高频业务场景
Deepin compiles VirtualBox records and resolves compilation errors
MOSFET 9n90 parameters of asemi, 9n90 circuit diagram, 9n90 physical diagram
Esp32-c3 single fire wire intelligent switch enables intelligent upgrading of traditional switch
Low code analysis and inventory: two misunderstandings need to be avoided in the application of low code in the banking industry
Two software test questions
[win] the browser cannot open the web page and DNS appears_ PROBE_ FINISHED_ NO_ INTERNET
Wechat applet saves video to photo album wx saveVideoToPhotosAlbum()
[group planning final review iv] input / output system
Chicken and rabbit in the same cage
【LeetCode】114. Expand binary tree into linked list
MySql安装+测试连接+常用终端mysql命令(超详细)