zoukankan      html  css  js  c++  java
  • 977. 有序数组的平方

    地址:https://leetcode-cn.com/problems/squares-of-a-sorted-array/

    <?php
    /**
     * Created by PhpStorm.
     * User: huahua
     * Date: 2020/10/19
     * Time: 下午4:52
    给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。
    
     
    
    示例 1:
    
    输入:[-4,-1,0,3,10]
    输出:[0,1,9,16,100]
    示例 2:
    
    输入:[-7,-3,2,3,11]
    输出:[4,9,9,49,121]
     
    
    提示:
    
    1 <= A.length <= 10000
    -10000 <= A[i] <= 10000
    A 已按非递减顺序排序。
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/squares-of-a-sorted-array
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
     */
    class Solution {
    
        /**
         * @param Integer[] $A
         * @return Integer[]
         */
        function sortedSquares($A) {
            $res = [];
            $count = count($A);
            $posIndex = 0;
            $negIndex  = 0;
            while($posIndex<$count && $A[$posIndex]<0){
                $posIndex++;
            }
            $negIndex = $posIndex--;
            for($i=0;0<=$posIndex&&$negIndex<$count;$i++){
                $negPow = $A[$negIndex]*$A[$negIndex];
                $posPow  = $A[$posIndex]*$A[$posIndex];
                if($negPow<$posPow){
                    $res[] = $negPow;
                    $negIndex++;
                }else{
                    $res[] = $posPow;
                    $posIndex--;
                }
            }
            while($negIndex<$count){
                $res[] = $A[$negIndex]*$A[$negIndex];
                $negIndex++;
            }
            while($posIndex>=0){
                $res[] = $A[$posIndex]*$A[$posIndex];
                $posIndex--;
            }
            return $res;
        }
    }
  • 相关阅读:
    Google笔试题
    OpenStack Ceilometer简介
    OpenStack Object Storage(Swift)架构、原理及特性
    【大话存储2读书笔记】磁盘IO的重要概念
    递归,汉诺塔游戏
    函数定义与使用
    字符串操作
    for循环:用turtle画一颗五角星
    列表、元组、字典、集合的定义、操作与综合练习
    npm 相关
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/13841139.html
Copyright © 2011-2022 走看看