Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
3a76b3a
更新 0704.二分查找 排版格式
jinbudaily Jul 17, 2023
948aca8
更新 0704.二分查找 排版格式修复
jinbudaily Jul 17, 2023
60e2391
Merge pull request #2183 from jinbudaily/master
youngyangyang04 Jul 17, 2023
fdbc744
更新 027.移除元素 排版格式修复
jinbudaily Jul 17, 2023
ec101be
更新 0977.有序数组的平方
jinbudaily Jul 17, 2023
76c84ef
更新 0209. 长度最小的子数组 排版格式修复
jinbudaily Jul 17, 2023
7a30d50
更新 059.螺旋矩阵II 排版格式修复
jinbudaily Jul 17, 2023
053632c
更新 数组总结篇 排版格式修复
jinbudaily Jul 17, 2023
a3cc34e
更新 数组理论基础 排版格式修复
jinbudaily Jul 17, 2023
23c558f
Merge branch 'master' of github.com:jinbudaily/leetcode-master
jinbudaily Jul 17, 2023
ae4300d
Merge pull request #2184 from jinbudaily/master
youngyangyang04 Jul 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
更新 0977.有序数组的平方
  • Loading branch information
jinbudaily committed Jul 17, 2023
commit ec101bebb24b9af7910c02b979b198c238ae12b1
42 changes: 25 additions & 17 deletions problems/0977.有序数组的平方.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
* 输入:nums = [-7,-3,2,3,11]
* 输出:[4,9,9,49,121]

# 算法公开课
## 算法公开课

**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。


# 思路
## 思路

## 暴力排序
### 暴力排序

最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下:

Expand All @@ -47,7 +47,7 @@ public:

这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。

## 双指针法
### 双指针法

数组其实是有序的, 只不过负数平方之后可能成为最大数了。

Expand Down Expand Up @@ -99,7 +99,8 @@ public:

## 其他语言版本

Java:
### Java:

```Java
class Solution {
public int[] sortedSquares(int[] nums) {
Expand Down Expand Up @@ -141,7 +142,8 @@ class Solution {
}
```

Python:
### Python:

```Python
(版本一)双指针法
class Solution:
Expand Down Expand Up @@ -176,7 +178,8 @@ class Solution:
return sorted(x*x for x in nums)
```

Go:
### Go:

```Go
func sortedSquares(nums []int) []int {
n := len(nums)
Expand All @@ -196,7 +199,8 @@ func sortedSquares(nums []int) []int {
return ans
}
```
Rust
### Rust:

```rust
impl Solution {
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
Expand All @@ -217,7 +221,8 @@ impl Solution {
}
}
```
Javascript:
### Javascript:

```Javascript
/**
* @param {number[]} nums
Expand All @@ -242,7 +247,7 @@ var sortedSquares = function(nums) {
};
```

Typescript:
### Typescript:

双指针法:

Expand Down Expand Up @@ -277,7 +282,7 @@ function sortedSquares(nums: number[]): number[] {
};
```

Swift:
### Swift:

```swift
func sortedSquares(_ nums: [Int]) -> [Int] {
Expand Down Expand Up @@ -305,7 +310,7 @@ func sortedSquares(_ nums: [Int]) -> [Int] {
}
```

Ruby:
### Ruby:

```ruby
def sorted_squares(nums)
Expand All @@ -323,8 +328,8 @@ def sorted_squares(nums)
end
```

### C:

C:
```c
int* sortedSquares(int* nums, int numsSize, int* returnSize){
//返回的数组大小就是原数组大小
Expand Down Expand Up @@ -357,7 +362,8 @@ int* sortedSquares(int* nums, int numsSize, int* returnSize){
}
```

PHP:
### PHP:

```php
class Solution {
/**
Expand Down Expand Up @@ -386,7 +392,7 @@ class Solution {
}
```

Kotlin:
### Kotlin:

双指针法
```kotlin
Expand Down Expand Up @@ -437,7 +443,7 @@ class Solution {
}
```

Scala:
### Scala:

双指针:
```scala
Expand Down Expand Up @@ -473,7 +479,8 @@ object Solution {
}
```

C#:
### C#:

```csharp
public class Solution {
public int[] SortedSquares(int[] nums) {
Expand Down Expand Up @@ -504,3 +511,4 @@ public class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

AltStyle によって変換されたページ (->オリジナル) /