{"id":8850,"date":"2021-11-28T08:33:35","date_gmt":"2021-11-28T16:33:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8850"},"modified":"2021-11-28T08:41:08","modified_gmt":"2021-11-28T16:41:08","slug":"leetcode-2089-find-target-indices-after-sorting-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2089-find-target-indices-after-sorting-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2089. Find Target Indices After Sorting Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;and a target element&nbsp;<code>target<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>target index<\/strong>&nbsp;is an index&nbsp;<code>i<\/code>&nbsp;such that&nbsp;<code>nums[i] == target<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list of the target indices of<\/em>&nbsp;<code>nums<\/code>&nbsp;after<em>&nbsp;sorting&nbsp;<\/em><code>nums<\/code><em>&nbsp;in&nbsp;<strong>non-decreasing<\/strong>&nbsp;order<\/em>. If there are no target indices, return&nbsp;<em>an&nbsp;<strong>empty<\/strong>&nbsp;list<\/em>. The returned list must be sorted in&nbsp;<strong>increasing<\/strong>&nbsp;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> nums = [1,2,5,2,3], target = 2\n<strong>Output:<\/strong> [1,2]\n<strong>Explanation:<\/strong> After sorting, nums is [1,<strong>2<\/strong>,<strong>2<\/strong>,3,5].\nThe indices where nums[i] == 2 are 1 and 2.\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> nums = [1,2,5,2,3], target = 3\n<strong>Output:<\/strong> [3]\n<strong>Explanation:<\/strong> After sorting, nums is [1,2,2,<strong>3<\/strong>,5].\nThe index where nums[i] == 3 is 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,5,2,3], target = 5\n<strong>Output:<\/strong> [4]\n<strong>Explanation:<\/strong> After sorting, nums is [1,2,2,3,<strong>5<\/strong>].\nThe index where nums[i] == 5 is 4.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,5,2,3], target = 4\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> There are no elements in nums with value 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i], target &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<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\nclass Solution {\npublic:\n  vector<int> targetIndices(vector<int>& nums, int target) {\n    sort(begin(nums), end(nums));\n    vector<int> ans;\n    for (int i = 0; i < nums.size(); ++i)\n      if (nums[i] == target) ans.push_back(i);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;and a target element&nbsp;target. A&nbsp;target index&nbsp;is an index&nbsp;i&nbsp;such that&nbsp;nums[i] == target. Return&nbsp;a list of the target indices of&nbsp;nums&nbsp;after&nbsp;sorting&nbsp;nums&nbsp;in&nbsp;non-decreasing&nbsp;order. If there are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,15],"class_list":["post-8850","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8850","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=8850"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8850\/revisions"}],"predecessor-version":[{"id":8852,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8850\/revisions\/8852"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}