{"id":4663,"date":"2019-01-20T00:24:32","date_gmt":"2019-01-20T08:24:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4663"},"modified":"2019-01-20T00:41:09","modified_gmt":"2019-01-20T08:41:09","slug":"leetcode-977-squares-of-a-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-977-squares-of-a-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 977. Squares of a Sorted Array"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>A<\/code>&nbsp;sorted in non-decreasing order,&nbsp;return an array of the squares of each number,&nbsp;also in sorted non-decreasing order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[-4,-1,0,3,10]\n<strong>Output: <\/strong>[0,1,9,16,100]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[-7,-3,2,3,11]\n<strong>Output: <\/strong>[4,9,9,49,121]\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li><li><code>-10000 &lt;= A[i] &lt;= 10000<\/code><\/li><li><code>A<\/code>&nbsp;is sorted in non-decreasing order.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Two pointers + Merge two sorted arrays<\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">c++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 120 ms\nclass Solution {\npublic:\n  vector<int> sortedSquares(vector<int>& A) {\n    vector<int> ans(A.size());\n    auto e = prev(end(A));\n    auto s = begin(A);\n    auto it = end(ans);\n    while (--it >= begin(ans)) {\n      if (e > s && abs(*e) > abs(*s))\n        *it = pow(*e--, 2);        \n      else\n        *it = pow(*s++, 2);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;A&nbsp;sorted in non-decreasing order,&nbsp;return an array of the squares of each number,&nbsp;also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[20,185,175],"class_list":["post-4663","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-array","tag-sorted","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4663"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4663\/revisions"}],"predecessor-version":[{"id":4667,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4663\/revisions\/4667"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}