NC366. 平方升序数组
描述
示例1
输入:
[1,0,1,2,3]
输出:
[0,1,1,4,9]
示例2
输入:
[5,4,3,2,1]
输出:
[1,4,9,16,25]
C++ 解法, 执行用时: 29ms, 内存消耗: 3648KB, 提交时间: 2022-03-15
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> sortedArray(vector<int>& nums) { vector<int> res; int index = 0; for (int i = 0; i < nums.size() - 1; ++i) { if (abs(nums[i]) > abs(nums[i + 1])) index = i + 1; } res.push_back(nums[index] * nums[index]); int left = index - 1, right = index + 1; while (left >= 0 && right < nums.size()) { if (abs(nums[left]) < abs(nums[right])) { res.push_back(nums[left] * nums[left]); --left; } else { res.push_back(nums[right] * nums[right]); ++right; } } while (left >= 0) { res.push_back(nums[left] * nums[left]); --left; } while (right < nums.size()) { res.push_back(nums[right] * nums[right]); ++right; } return res; } };
C++ 解法, 执行用时: 30ms, 内存消耗: 3796KB, 提交时间: 2022-04-18
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> sortedArray(vector<int>& nums) { // write code here vector<int> res; int idx = 0; int min_value = INT_MAX; for (int i = 0; i < nums.size(); i++) { if (abs(nums[i]) < min_value) { min_value = abs(nums[i]); idx = i; } } res.push_back(min_value * min_value); int left = idx - 1, right = idx + 1; while (left >= 0 && right < nums.size()) { if (abs(nums[left]) < abs(nums[right])) { res.push_back(nums[left] * nums[left]); left -= 1; } else { res.push_back(nums[right] * nums[right]); right += 1; } } while (left >= 0) { res.push_back(nums[left] * nums[left]); left -= 1; } while (right < nums.size()) { res.push_back(nums[right] * nums[right]); right += 1; } return res; } };
C 解法, 执行用时: 31ms, 内存消耗: 3208KB, 提交时间: 2022-03-31
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 * * C语言声明定义全局变量请加上static,防止重复定义 */ int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int* sortedArray(int* nums, int numsLen, int* returnSize ) { // write code here int *ans = (int *)malloc(sizeof(int) * numsLen); *returnSize = 0; /* for (int i = 0; i < numsLen; ++i) ans[(*returnSize)++] = nums[i] * nums[i]; qsort(ans, numsLen, sizeof(int), cmp); */ int index = 0; int min_index = 0; for (int i = 0; i < numsLen - 1; ++i) { if (abs(nums[i]) < abs(nums[i + 1])) { min_index = i; break; } } *returnSize = 0; ans[(*returnSize)++] = nums[min_index] * nums[min_index]; int left = min_index - 1; int right = min_index + 1; while (left >= 0 && right < numsLen) { if (abs(nums[left]) <= abs(nums[right])) { ans[(*returnSize)++] = nums[left] * nums[left]; --left; } else { ans[(*returnSize)++] = nums[right] * nums[right]; ++right; } } while (left >= 0) { ans[(*returnSize)++] = nums[left] * nums[left]; --left; } while (right < numsLen) { ans[(*returnSize)++] = nums[right] * nums[right]; ++right; } return ans; }
C++ 解法, 执行用时: 31ms, 内存消耗: 3776KB, 提交时间: 2022-08-06
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> sortedArray(vector<int>& nums) { // write code here vector<int> res; int index=0; for(int i=0;i<nums.size()-1;++i) { if(abs(nums[i])>abs(nums[i+1])) index=i+1; } res.push_back(nums[index]*nums[index]); int left=index-1,right=index+1; while(left>=0&&right<nums.size()) { if(abs(nums[left])<abs(nums[right])) { res.push_back(nums[left]*nums[left]); --left; }else{ res.push_back(nums[right]*nums[right]); ++right; } } while(left>=0) { res.push_back(nums[left]*nums[left]); --left; } while(right<nums.size()) { res.push_back(nums[right]*nums[right]); ++right; } return res; } };
C++ 解法, 执行用时: 32ms, 内存消耗: 3304KB, 提交时间: 2022-03-15
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector<int> sortedArray(vector<int>& nums) { for(auto &n:nums) n=n*n; sort(nums.begin(),nums.end()); return nums; } };