题目标签: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. android cmd adb命令安装和删除apk应用

    copy自http://blog.csdn.net/xpsharp/article/details/7289910 1. 安装Android应用程序 1) 启动Android模拟器 2) adb in ...

  2. Android开发笔记(1)——View

    笔记链接:http://www.cnblogs.com/igoslly/p/6781592.html   一.View基础知识            IDE——Integrated Developme ...

  3. 安卓TV盒子常见问题以及解决方法

    1.为什么requestfocus无效 原因:requestfocus不支持在Touch模式下的Focus; 解法方案:再加一个requestFocusFromTouch函数. 2.摄像头打开问题,调 ...

  4. Verilog之event

    1 Explicit event The value changes on nets and variable can be used as events to trigger the executi ...

  5. jsp学习笔记 - 内置对象 session

    1.session 主要用来用户的登录和注销 设置用户名,获取用户名 session.setAttribute("username","johnson"); s ...

  6. apache自带的ab压力测试工具

    httpd-2.4.27-Win64-VC15 链接: https://pan.baidu.com/s/1027MtVwbq1zjUgF7P7Rrkw 密码: ne6a 下载解压后doc窗口cd .. ...

  7. 13EL表达式语言

    EL表达式语言 EL表达式语言 JSP用于在页面上显示动态内容,通常需要在JSP页面中嵌入Java脚本以完成复杂功能.但大量的Java脚本使得JSP页面难以维护.一种类似JavaScript语言—EL ...

  8. 小程序wx:key = “{{*this}}”报错

    解决方案:改为 wx:key = "*this"

  9. 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)

    Dijkstra+ 链式前向星+ 优先队列   Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...

  10. 题解 [USACO18DEC]Balance Beam

    被概率冲昏的头脑~~~ 我们先将样例在图上画下来: 会发现,最大收益是: 看出什么了吗? 这不就是凸包吗? 跑一遍凸包就好了呀,这些点中,如果i号点是凸包上的点,那么它的ans就是自己(第二个点),不 ...