您的位置:首页 > 汽车 > 时评 > 广告投放基础知识_seo竞争对手分析_广告推广怎么做最有效_关键词排名怎么查

广告投放基础知识_seo竞争对手分析_广告推广怎么做最有效_关键词排名怎么查

2025/6/21 1:48:19 来源:https://blog.csdn.net/2403_83306207/article/details/146405544  浏览:    关键词:广告投放基础知识_seo竞争对手分析_广告推广怎么做最有效_关键词排名怎么查
广告投放基础知识_seo竞争对手分析_广告推广怎么做最有效_关键词排名怎么查

977. 有序数组的平方 - 力扣(LeetCode)

方法一:暴力循环

先遍历所有的数,然后用Sort排序

class Solution {public int[] sortedSquares(int[] nums) {int[] result = new int[nums.length];for (int i = 0; i < nums.length; i++) {result[i] = nums[i] * nums[i];}Arrays.sort(result);return result;}
}

方法二 双指针

因为原数组是有顺序的,利用双指针,判断两个数的大小,放入新的数组即可

class Solution {public int[] sortedSquares(int[] nums) {int[] result = new int[nums.length];int l = 0;int r = nums.length - 1;int index = nums.length-1;while (l <= r) {if (nums[r] * nums[r] >= nums[l] * nums[l]){result[index] = nums[r] * nums[r];r--;}else{result[index] = nums[l] * nums[l];l++;}index--;}return result;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com