题目标签: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. 在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法

    在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法 [MXDRAW CAD控件文档] 下面帮助的完整例子,在控件安装目录的Sample\Ie\iedemo.htm中. 1.      主要用到 ...

  2. java基础学习之垃圾回收机制

    回收过程: 1.发现无用的对象 2.回收无用对象占用的内存的空间. 垃圾回收相关算法: 1.引用计数法 堆中每个对象都有一个引用计数.被引用一次,计数加一.被引用变量值变为null,则计数减一. 到计 ...

  3. Spring框架系列(一)--Spring MVC基础知识

    Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...

  4. 有关bash,我希望我能知晓的十件事

    简介 我之前的一篇文章比我预想的更受欢迎,因此我想再写一篇文章来介绍一些不太知名的bash功能 正如之前所言,由于我觉得bash是一种要经常使用(且需理解)的技术,所以我在研究bash时写了一本书.虽 ...

  5. php第二十八节课

    文件上传 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  6. 10JDBC、CURD、XML、XPath

    10JDBC.CURD.XML.XPath-2018/07/20 1.JDBC JDBC:java database connectivity JDBC与数据库驱动的关系:接口与实现的关系. JDBC ...

  7. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  8. Openstack manila的一些命令

    (本文是测试环境进行的操作:) 1.查看一些信息: [root@openstackcontroller ~]# manila type-list [root@openstackcontroller ~ ...

  9. Java核心技术 卷一 复习笔记(乙

    1.字符串从概念上讲,Java字符串就是Unicode字符序列.Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类,叫String. 每个用双引号括起来的字符串都是 String ...

  10. 洛谷 2476 [SCOI2008]着色方案

    50%的数据满足:1 <= k <= 5, 1 <= ci <= 3 100%的数据满足:1 <= k <= 15, 1 <= ci <= 5 [题解] ...