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]
 class Solution {
public int[] sortedSquares(int[] A) {
int n = A.length;
int[] result = new int[n];
int i = , j = n - ;
for (int p = n - ; p >= ; p--) {
if (Math.abs(A[i]) > Math.abs(A[j])) {
result[p] = A[i] * A[i];
i++;
} else {
result[p] = A[j] * A[j];
j--;
}
}
return result;
}
}
 

Squares of a Sorted Array的更多相关文章

  1. LeetCode977.Squares of a Sorted Array

    题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...

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

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

  3. [Swift]LeetCode977. 有序数组的平方 | Squares of a Sorted Array

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

  4. #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- ...

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

  6. Squares of a Sorted Array LT977

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

  7. 977. Squares of a Sorted Array

    题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  8. 【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 ...

  9. 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

随机推荐

  1. Connect AS400 through firewall(JDBC will require ports: 449, 8470, 8471, and 8476)

    What TCP ports are used by ODBC to connect to the DB2/400?  8471/9471 http://search400.techtarget.co ...

  2. HGOI20191114 CSP模拟赛 反思

    Problem A 宇宙魔方 有一个$N \times N \times N$的魔方,每一次操作可以整体转动该魔方,也可以对于一层整体+X. 给出最后魔方的最终状态,其中有一个位置为-1.利用其它位置 ...

  3. python一些问题

    1.对于字符变量来说不需要深度复制,字符变量是不能改变的 2.文件读取结尾的判断是通过判读 line=self.fd.readline() if not line: //结束了 不用通过判断字符长度. ...

  4. Java 面试题 三 <JavaWeb应用调优线程池 JVM原理及调优>

    1.Java Web应用调优线程池 不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文由浅入深,介 ...

  5. Python数据抓取(3) —抓取标题、时间及链接

    本次分享,jacky将跟大家分享如何将第一财经文章中的标题.时间以及链接抓取出来 (一)观察元素抓取位置 网页的原始码很复杂,我们必须找到特殊的元素做抽取,怎么找到特殊的元素呢?使用开发者工具检视每篇 ...

  6. 最近公共祖先lca模板

    void dfs(int x,int root){//预处理fa和dep数组 fa[x][0]=root; dep[x]=dep[root]+1; for(int i=1;(1<<i)&l ...

  7. The JAVA_HOME environment variable is not defined correctly的错误

    The JAVA_HOME environment variable is not defined correctlyThis environment variable is needed to ru ...

  8. Leetcode题目160.相交链表(简单)

    题目描述 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = ...

  9. FreeMarker学习(内建函数参考)

    内容参考:http://freemarker.foofun.cn/dgui_quickstart_basics.html 一.字符串内建函数 boolean: 字符串转为布尔值.字符串必须是 true ...

  10. 数据可视化界面UI设计大屏展示