Last active
July 18, 2022 08:06
-
-
Save icameling/9085aedf4b7db9c919f39456a880731e to your computer and use it in GitHub Desktop.
#数组 #有序数据的平方 #leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> sortedSquares(vector<int>& nums) { | |
int i = 0, j = nums.size() - 1; | |
int k = 0; | |
vector<int> rets(nums.size(), 0); | |
for (int k = nums.size() - 1; k >=0; k--) { | |
int ii = nums[i] * nums[i]; | |
int jj = nums[j] * nums[j]; | |
if ( ii > jj) { | |
rets[k] = ii; | |
i++; | |
} else { | |
rets[k] = jj; | |
j--; | |
} | |
} | |
return rets; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment