Leetcode 977. Squares of a Sorted Array, Java解法

S32K344开发环境搭建全攻略:S32DS 3.5.12与RTD 3.0.0实战指南 在嵌入式开发中,集成开发环境(IDE)与实时驱动(RTD)的协同配置是项目启动的关键。S32DS作为NXP官方IDE,结合RTD包,为S32K3系列提供底层驱动支持。正确安装与路径配置能确保编译、调试与烧录流程顺畅,避免常见错误。该环境适用于汽车电子、工业控制等场景,如车身域控、BMS。本文基于S32K344芯片,详细记录从软件下载到调试的完整过程,帮助开发者快速搭建稳定开发环境。 阅读详情

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]

Example 2:

Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]

Note:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. A is sorted in non-decreasing order.

Approach #1 我的解法 Array.sort() method

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

用Array.sort() method.

Complexity Analysis

  • Time Complexity: O(N log N), where N is the length of A.

  • Space Complexity: O(N). 

Approach #2 Two Pointer

class Solution {
    public int[] sortedSquares(int[] A) {
        int N = A.length;
        int j = 0;
        while (j < N && A[j] < 0)
            j++;
        int i = j-1; //最后一个负数的下标

        int[] ans = new int[N];
        int t = 0;

        while (i >= 0 && j < N) { //从正负数的分界开始,坐标向两边移动
            if (A[i] * A[i] < A[j] * A[j]) {
                ans[t++] = A[i] * A[i];
                i--;
            } else {
                ans[t++] = A[j] * A[j];
                j++;
            }
        }
        
        // 最后加入剩下的
        while (i >= 0) {
            ans[t++] = A[i] * A[i];
            i--;
        }
        while (j < N) {
            ans[t++] = A[j] * A[j];
            j++;
        }

        return ans;
    }
}

 This strategy is to iterate over the negative part in reverse, and the positive part in the forward direction.

We can use two pointers to read the positive and negative parts of the array - one pointer j in the positive direction, and another i in the negative direction.

Now that we are reading two increasing arrays (the squares of the elements), we can merge these arrays together using a two-pointer technique.

Complexity Analysis

  • Time Complexity: O(N), where N is the length of A.

  • Space Complexity: O(N)

CUDA详细安装及环境配置——环境配置指南 – CUDA+cuDNN+PyTorch 安装 本文详细介绍了如何确定显卡支持的CUDA版本、选择对应的cuDNN版本,并逐步安装CUDA、cuDNN和PyTorch。首先,通过命令行输入nvidia-smi.exe查看显卡支持的CUDA版本。接着,根据CUDA版本选择对应的cuDNN版本并下载。然后,从NVIDIA官方网站下载CUDA安装包,按照提示进行安装,并检查是否安装成功。同样地,下载cuDNN安装包,解压后将文件拷贝到CUDA安装目录中,并通过命令行验证安装是否成功。最后,根据PyTorch官网的指引,选择合适的安装命令进行PyTorch的安装 阅读详情

相关推荐

告别jom Error 2:深入理解QtCreator、CMake与MSVC工具链的协作机制

本文深入解析QtCreator构建过程中常见的jom Error 2问题,揭示CMake与MSVC工具链的协作机制。从CMake生成器的工作原理到MSVC工具链的组件协作,帮助开发者系统化诊断构建错误,提升开发效率。

weixin_30654419的博客 429

LeetCode 977. Squares of a Sorted Array 有序数组的平方 JAVA实现

题目内容: 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。   示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100] 示例 2: 输入:[-7,-3,2,3,11] 输出:[4,9,9,49,121]   提示: 1 &lt;= A.length &lt;= 10000 -10000 &lt;= A...

帽帽的博客 776

NSGA(非支配排序遗传算法)

在多目标优化问题中,往往存在多个相互冲突的目标需要同时优化。传统的单目标优化算法无法直接应用于这类问题,因此需要专门的多目标优化算法。非支配排序遗传算法(Non-dominated Sorting Genetic Algorithm,NSGA)是一种经典的多目标优化算法,由印度科学家 Kalyanmoy Deb 等人于 1995 年提出。NSGA 通过非支配排序和拥挤度距离来引导种群向 Pareto 最优前沿进化,从而得到一组具有良好分布性和收敛性的 Pareto 最优解。

2970

Java/977. Squares of a Sorted Array 有序数组的平方

题目 代码部分(7ms 92.26%) class Solution { public int[] sortedSquares(int[] A) { int len = A.length; int[] res = new int[len]; for(int i = 0; i < l...

qq_38959715的博客 1420

测绘无人船避坑指南:云卓H12Pro遥控器接海康摄像头,从网络设置到RTSP流延迟优化全流程

本文详细解析了测绘无人船视频子系统的优化方案,重点介绍海康威视摄像头与云卓H12Pro遥控器的深度适配流程。从网络设置到RTSP流延迟优化,提供了一套经过实测的低延迟(<200ms)解决方案,适用于复杂水域环境下的测绘任务。

weixin_30415113的博客 625

集成学习综述—bagging/随机森林/adaboost/GBDT/XGBoost/LightGBM/catboost 原理和代码介绍

集成学习介绍 文章目录集成学习介绍1 基本概念1.1 定义基本定义集成学习类别参考链接1.2 基学习器(CART)介绍公式介绍例子讲解代码实践参考链接1.3 偏差(bias)和方差(var)公式推导图解参考链接2 Boosting类2.1 原理2.2 AdaBoost2.2.2 公式推导2.2.3 优点与不足优点不足应用2.2.4 代码实践2.2.5 参考链接2.3 GBDT原理介绍代码实践参考...

fzl的博客 4615

Leetcode 977. Squares of a Sorted Array

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.   Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,...

Dive Deep into The Knowledge 716

手把手教你构建轻量级CLIP模型:从理论到实践

本文手把手教你从零构建一个轻量级CLIP模型,深入解析其核心原理——对比学习,并提供完整的代码实践。通过精简的模型架构、数据准备和训练循环实现,帮助读者彻底理解多模态模型如何让图像与文本在共享空间中对齐,为学习更复杂的AI模型打下坚实基础。

weixin_29100927的博客 325

Median of Two Sorted Array leetcode java

题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).   题解:首先我们先明确什么是median,即中位数。 ...

weixin_34001430的博客 172

PIVlab软件入门:从GUI操作到2D2C粒子测速实战

本文详细介绍了PIVlab软件在2D2C粒子测速中的入门与实战应用。从GUI操作到环境搭建,再到图像预处理和核心参数设置,帮助用户快速掌握这一基于MATLAB的开源工具。通过实际案例和技巧分享,展示了PIVlab在流体力学实验中的高效性和实用性。

weixin_42547431的博客 227

Mac 安装neo4j(解压版)最新版本教程

本文介绍了在Mac M系列操作系统上安装Neo4j图数据库的具体步骤。内容包括:从官网下载解压版Neo4j社区版压缩包;通过终端命令将其解压到/opt目录并重命名;配置环境变量以便全局访问;以及常用命令如启动/停止服务、重置密码等。安装完成后,用户可通过浏览器访问http://localhost:7474,使用默认账号neo4j/neo4j登录并根据提示重置密码。该教程简洁明了地指导用户完成Neo4j在Mac上的安装配置过程。

猿小白的博客 909

977. Squares of a Sorted Array

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,1...

同销万古愁的博客 452

华为OD机试 - 服务中心选址(Python)

一个快递公司希望在一条街道建立新的服务中心。公司统计了该街道中所有区域在地图上的位置,并希望能够以此为依据为新的服务中心选址:使服务中心到所有区域的距离的总和最小。先输入区域数组positions的长度n(1 ≤ n ≤ 10^5)接下来 n 行每行输入成对的left和right值,以空格隔开。输出为location。

m0_46181452的博客 282

Leetcode 977. Squares of a Sorted Array (Java) 错误accept解法,一定要小心

Given an integer arraynumssorted innon-decreasingorder, returnan array ofthe squares of each numbersorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16...

weixin_58080442的博客 125

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【LeetCode】977. Squares of a Sorted Array

原题网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 解题思路: 我的解:毫无疑问的暴力解。。。。 public int[] sortedSquares(int[] A) { int len = A.length; int[] B = new int[len]; f...

zzf_forgot的博客 199

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LeetCode C++ 977. Squares of a Sorted Array【Two Pointers】简单

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: Input: [-7,-3,2,3,11] Output: [4,9,9,49,121

memcpy0的博客 431

【leetcode】977. Squares of a Sorted Array

思路 从数组左右两边同时开始,因为数组初始有序,所以可以直接比较绝对值大小,大的放在结果数组最后,依次排到最前。 class Solution { public int[] sortedSquares(int[] A) { int[] result = new int[A.length]; for (int i=0,j=A.le...

Art1st_D的博客 203

Java描述 LeetCode,977. Squares of a Sorted Array 有序数组的平方

大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。 1-1:题目描述 Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasin.

人在江湖 332

leetcode977-Squares of a Sorted Array

这道题目用双指针遍历可以很好的解决,因为平方以后最小的元素肯定是在中间,所以每次只要比较首尾俩个元素的大小取最大值即可。给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。解释:平方后,数组变为 [16,1,0,9,100]输入:nums = [-4,-1,0,3,10]排序后,数组变为 [0,1,9,16,100]输出:[0,1,9,16,100]

我的程序人生 640

LeetCode 977. Squares of a Sorted Array (有序数组的平方)

题目标签:Array   题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达。   因为有负数在里面,平方后,负数在array的位置会变动。   可以设left 和 right pointers,从两边遍历,比较一下两个平方后的数字,把大的那个 放入新建的array的末尾。   具体看code。 Java Solution: Runti...

weixin_30344995的博客 112

[leetcode刷题] [Easy] [Array] 977. 有序数组的平方(Squares of a Sorted Array)- Java

题目: Squares of a Sorted Array 英文: Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: Input: [-

lxfHaHaHa的博客 373

Leetcode 977 Squares of a Sorted Array

class Solution { public int[] sortedSquares(int[] A) { int len=A.length; for(int i=0;i&lt;len;++i){ A[i]=A[i]*A[i]; } Arrays.sort(A); return A;...

Polanwind 162

leetcode刷题记录 easy(8) 977.Squares of a Sorted Array

英文题目: Given an array of integersAsorted in non-decreasing order,return an array of the squares of each number,also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10]Output: [0,1,...

小魑魅的博客 215

Leetcode 977. Squares of a Sorted Array (Java)

我会陆续更新leetcode题目的答案,语言主要选择的是java和python,答案为普通解法,时间和空间复杂度并非最佳,但愿意分享给大家,有更优解朋友,欢迎分享到评论区。谢谢。

weixin_58080442的博客 138

leetcode-day3:Squares of a Sorted Array(有序数组的平方)

数组:有序数组的平方,还有序么? 977. Squares of a Sorted Array LeetCode题目链接 Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16

丹辰子的博客 1107
上一篇: Leetcode 905. Sort Array By Parity, Java解法
下一篇: Leetcode 561. Array Partition I, Java解法
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值