首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
平方升序数组
[编程题]平方升序数组
热度指数:1249
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个长度为 n 的升序数组 nums ,请你算出这个数组中所有数的平方组成的新的数组,要求也按照升序。
数据范围:
,数组中的数都满足
示例1
输入
[1,0,1,2,3]
输出
[0,1,1,4,9]
示例2
输入
[5,4,3,2,1]
输出
[1,4,9,16,25]
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(11)
分享
纠错
提交结果有问题?
2个回答
6篇题解
添加回答
0
Python3
响亮的闵玆
class
Solution
:
def
sortedArray
(
self
,
nums
: List[
int
]):
a,b=
0
,
len
(nums)
a1,c=b-
1
,b-
1
res=[
float
(
'inf'
)]*b
while
c>=
0
:
if
nums[a]*nums[a]< nums[a1]*nums[a1] :
res[c]=nums[a1]*nums[a1]
a1-=
1
else
:
res[c]=nums[a]*nums[a]
a+=
1
c-=
1
return
res
编辑于 2024-02-15 23:16:59
回复(0)
0
Python3
服了你个老六的比尔很迟缓
记得把第三行注释掉
class
Solution
:
def
sortedArray
(
self
,
nums
: List[
int
]) -> List[
int
]:
# write code here
news=[]
for
i
in
nums:
news.append(
pow
(i,
2
))
news.sort()
return
news
发表于 2023-08-21 21:36:46
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
排序
双指针
难度:
2条回答
11收藏
2124浏览
热门推荐
通过挑战的用户
查看代码
半个西瓜半个夏
2022-09-15 10:15:40
commoand
2022-09-13 17:22:26
牛客41342...
2022-09-09 16:27:22
刘SS
2022-09-06 16:10:29
在开会的回宇同...
2022-09-06 16:06:20
相关试题
最小面积子矩阵
动态规划
双指针
前缀和
评论
(44)
和为S的两个数字
数组
数学
双指针
评论
(1508)
来自
“一战通offer”互联...
神奇的数字
排序
双指针
评论
(46)
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型ArrayList * @return int整型ArrayList */ public ArrayList
sortedArray (ArrayList
nums) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector
sortedArray(vector
& nums) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution: def sortedArray(self , nums ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public List
sortedArray (List
nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ function sortedArray( nums ) { // write code here } module.exports = { sortedArray : sortedArray };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution: def sortedArray(self , nums: List[int]) -> List[int]: # write code here
package main //import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ func sortedArray( nums []int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* sortedArray(int* nums, int numsLen, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution def sortedArray(nums) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ def sortedArray(nums: Array[Int]): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ fun sortedArray(nums: IntArray): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public int[] sortedArray (int[] nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ export function sortedArray(nums: number[]): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ func sortedArray ( _ nums: [Int]) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ pub fn sortedArray(&self, nums: Vec
) -> Vec
{ // write code here } }
[1,0,1,2,3]
[0,1,1,4,9]
[5,4,3,2,1]
[1,4,9,16,25]