题目标签:Array

  题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达。

  因为有负数在里面,平方后,负数在array的位置会变动。

  可以设left 和 right pointers,从两边遍历,比较一下两个平方后的数字,把大的那个 放入新建的array的末尾。

  具体看code。

Java Solution:

Runtime beats 100.00%

完成日期:02/03/2019

关键点:two pointers

 class Solution
{
public int[] sortedSquares(int[] A)
{
int left = 0;
int right = A.length - 1;
int[] result = new int[A.length];
int inputIndex = right; while(left <= right)
{
int leftInt = A[left] * A[left];
int rightInt = A[right] * A[right]; if(leftInt > rightInt)
{
result[inputIndex] = leftInt;
left++;
inputIndex--;
}
else
{
result[inputIndex] = rightInt;
right--;
inputIndex--;
}
}
return result;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 977. Squares of a Sorted Array (有序数组的平方)的更多相关文章

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

    网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 双指针法 把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并 ...

  2. #Leetcode# 977. Squares of a Sorted Array

    https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...

  3. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  4. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  5. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  6. 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 ...

  7. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

  8. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. jsp学习笔记 - 内置对象 application

    ---恢复内容开始--- 1.application一般用this.getServletContext()替代 2.appllication有一个非常有用的函数 getRealPath(),获取绝对路 ...

  2. C++_运算符重载 总结

    什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 或 友元函数(类外 ...

  3. Java基础(四)--接口和抽象类

    接口和抽象类能够体现OOP的抽象,而接口和抽象类也是日常开发中经常用到的 抽象方法: 抽象方法就是被abstract修饰的方法,只有声明,没有实现,也就是没有方法体 public abstract v ...

  4. BZOJ 1176: [Balkan2007]Mokia KDtree

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...

  5. 一个简单的java年龄计算器

    制作一个如下图年龄计算器 根据题目,我做了一个由Calendar类以及年月日各相减得到的年龄,当然正确的方法不止一个,以下为我的源代码和结果截图: package com.Date; import j ...

  6. Navicat for MySQL(Ubuntu)过期解决方法

    推荐购买正版软件,尊重版权  [官网在这里] Navicat for MySQL(Ubuntu系统)免费版试用过期解决方法: Step1. 直接删除 /home目录下的  .navicat文件夹(64 ...

  7. 解决window.location.href参数太长

    前言:一提到页面跳转,最常用的一般就是window.location.href,如果需要带参数,也许可以在后面用?拼上,但这样并不安全,而且有个更严重的问题,这样的拼接是有长度限制的,如果达到好几千个 ...

  8. npm 使用教程

    链接----------------------------------npm官网npm淘宝镜像 安装包----------------------------------npm install -g ...

  9. 充当别的mcu的外部存储器(51类)

    // 锁存地址 - STC12C5A60S2 reg [15:0]rAddr_51; //存放51单片机传过来的地址 读51地址寄存器 always @ (posedge MCLKout or neg ...

  10. asp.net mvc 4.0 新特性之移动特性

    asp.net mvc 4.0 新特性之移动特性 为不同的客户端提供不同的视图 手动重写 UserAgent,从而强制使用对应的视图 示例1.演示如何为不同的客户端提供不同的视图Global.asax ...