{"id":2753,"date":"2018-04-22T09:58:22","date_gmt":"2018-04-22T16:58:22","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2753"},"modified":"2018-04-22T10:23:07","modified_gmt":"2018-04-22T17:23:07","slug":"leetcode-821-shortest-distance-to-a-character","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-821-shortest-distance-to-a-character\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 821. Shortest Distance to a Character"},"content":{"rendered":"<div class=\"question-description\">\n<div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u5230\u4e00\u4e2a\u6307\u5b9a\u5b57\u7b26\u7684\u5bf9\u77ed\u8ddd\u79bb\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/shortest-distance-to-a-character\/description\/\">https:\/\/leetcode.com\/problems\/shortest-distance-to-a-character\/description\/<\/a><\/p>\n<p>Given a string\u00a0<code>S<\/code>\u00a0and a character\u00a0<code>C<\/code>, return an array of integers representing the shortest distance from the character\u00a0<code>C<\/code>\u00a0in the string.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> S = \"loveleetcode\", C = 'e'\r\n<strong>Output:<\/strong> [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>S<\/code>\u00a0string length is\u00a0in\u00a0<code>[1, 10000].<\/code><\/li>\n<li><code>C<\/code>\u00a0is a single character, and guaranteed to be in string\u00a0<code>S<\/code>.<\/li>\n<li>All letters in\u00a0<code>S<\/code>\u00a0and\u00a0<code>C<\/code>\u00a0are lowercase.<\/li>\n<\/ol>\n<\/div>\n<h1><strong>Solution: Two Pass<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 14 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; shortestToChar(string S, char C) {\r\n    const int n = S.length();\r\n    vector&lt;int&gt; ans(n, INT_MAX);\r\n    int index = -1;\r\n    for (int i = 0; i &lt; n; ++i) {\r\n      if (S[i] == C) index = i;\r\n      if (index &lt; 0) continue;\r\n      ans[i] = abs(i - index);\r\n    }\r\n    index = -1;\r\n    for (int i = n - 1; i &gt;= 0; --i) {\r\n      if (S[i] == C) index = i;\r\n      if (index &lt; 0) continue;\r\n      ans[i] = min(ans[i], abs(i - index));\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>V2<\/p>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\n\/\/ Running time: 14 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; shortestToChar(string S, char C) {\r\n    const int n = S.length();\r\n    vector&lt;int&gt; indices(2 * n); \/\/ 0, 1, ..., n - 1, n - 1, n - 2, ..., 0\r\n    std::iota(indices.begin(), indices.begin() + n, 0);\r\n    std::iota(indices.rbegin(), indices.rbegin() + n, 0);\r\n    vector&lt;int&gt; ans(n, INT_MAX);\r\n    int index = -n;\r\n    for (int i : indices) {\r\n      if (S[i] == C) index = i;\r\n      ans[i] = min(ans[i], abs(i - index));\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u5230\u4e00\u4e2a\u6307\u5b9a\u5b57\u7b26\u7684\u5bf9\u77ed\u8ddd\u79bb\u3002 https:\/\/leetcode.com\/problems\/shortest-distance-to-a-character\/description\/ Given a string\u00a0S\u00a0and a character\u00a0C, return an array of integers representing the shortest distance from the character\u00a0C\u00a0in the string. Example 1: Input:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[130,222,4],"class_list":["post-2753","post","type-post","status-publish","format-standard","hentry","category-string","tag-distance","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2753","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=2753"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2753\/revisions"}],"predecessor-version":[{"id":2758,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2753\/revisions\/2758"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}