当前位置:网站首页>leetcode977. Square of ordered array
leetcode977. Square of ordered array
2022-07-19 04:26:00 【pearz】
Address :https://leetcode.cn/problems/squares-of-a-sorted-array/
describe :
Here's a button Non decreasing order Sorted array of integers nums, return The square of each number A new array of , According to the requirements Non decreasing order Sort .
example 1:
Input :nums = [-4,-1,0,3,10]
Output :[0,1,9,16,100]
explain : After square , The array becomes [16,1,0,9,100]
After ordering , The array becomes [0,1,9,16,100]
example 2:
Input :nums = [-7,-3,2,3,11]
Output :[4,9,9,49,121]
violence
answer :
class Solution {
public int[] sortedSquares(int[] nums) {
int len = nums.length;
for (int i = 0; i < len; i++) {
nums[i] = nums[i] * nums[i];
}
Arrays.sort(nums);
return nums;
}
}
Space for time
answer :
class Solution {
public int[] sortedSquares(int[] nums) {
int len = nums.length;
int left = 0;
int right = len - 1;
int[] res = new int[len];
int loc = len - 1;
while (loc >= 0) {
if (nums[right] * nums[right] > nums[left] * nums[left]) {
res[loc--] = nums[right] * nums[right--];
} else {
res[loc--] = nums[left] * nums[left++];
}
}
return res;
}
}
边栏推荐
- C # use this keyword to concatenate constructor to call method
- 微信附近的人小程序怎么开(开通附近小程序的方法)
- leetcode209. 长度最小的子数组
- 基于STM32的SG90舵机实验含代码(HAL库)
- AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
- 64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m
- 小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能
- Chapter 6 performance platform godeye source code analysis - Custom expansion module
- 小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT
- Structure gets the address of the main structure (struct) through member variables
猜你喜欢

小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能

By voting for the destruction of STI by Dao, seektiger is truly community driven

Technical writing guide for programmers to leave work early

Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture

Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino

Wechat online education video on demand learning applet graduation project (4) opening report

How to filter viruses / spam more effectively!

Unity - how to modify a package or localize it

Wechat e-book reading applet graduation design of applet completion works (3) background function

Wechat e-book reading of small program graduation design (5) task book
随机推荐
小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
Unity - how to modify a package or localize it
CAD视频课程推荐 b站
High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
Wechat e-book reading applet graduation project (8) graduation project thesis template
小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT
About the problem of database, the concept of uniqueness and non repetition
[database] must know and be able at the end of the term ----- Chapter 1 database overview
Graphic verification code verification
Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
2022/7/16 周赛
Intensive reading series of papers
OSPF基础优化
leetcode7-dfs+动态规划+双指针
Laravel's file upload
Machine learning 09: unsupervised learning
06 Maui, WPF uses MVVM toolkit framework to build MVVM program
Intel helps open medical service and promote the intellectualization of ultrasonic prenatal examination
巧用企业网盘收取报告或总结