mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #396 from u4989190/master
添加 977.有序数组的平方 Javascript 版本
This commit is contained in:
@ -198,6 +198,31 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
```Javascript
|
||||
/**
|
||||
* @desc two pointers solution
|
||||
* @link https://leetcode-cn.com/problems/squares-of-a-sorted-array/
|
||||
* @param nums Array e.g. [-4,-1,0,3,10]
|
||||
* @return {array} e.g. [0,1,9,16,100]
|
||||
*/
|
||||
const sortedSquares = function (nums) {
|
||||
let res = []
|
||||
for (let i = 0, j = nums.length - 1; i <= j;) {
|
||||
const left = Math.abs(nums[i])
|
||||
const right = Math.abs(nums[j])
|
||||
if (right > left) {
|
||||
// push element to the front of the array
|
||||
res.unshift(right * right)
|
||||
j--
|
||||
} else {
|
||||
res.unshift(left * left)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
Reference in New Issue
Block a user