{"id":10252,"date":"2025-04-02T21:08:43","date_gmt":"2025-04-03T04:08:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10252"},"modified":"2025-04-04T11:38:05","modified_gmt":"2025-04-04T18:38:05","slug":"leetcode-2296-design-a-text-editor","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2296-design-a-text-editor\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2296 Design a Text Editor"},"content":{"rendered":"\n<p>\u65b9\u6cd51\uff1a\u53cc\u5411\u94fe\u8868\u6765\u5b58\u50a8\u6587\u672c\uff0c\u8fed\u4ee3\u5668\u6307\u5411\u5149\u6807\u6240\u5728\u7684\u4f4d\u7f6e\u3002\u52a0\u4e00\u4e2adummy head\u4f1a\u4f7f\u4ee3\u7801\u7b80\u5355\u5f88\u591a\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aTextEditor O(1) addText O(|text|) deleteText O(k) cursorLeft O(k) cursorRight(k)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)<\/p>\n\n\n\n<p>\u7531\u4e8e\u6bcf\u4e2a\u5b57\u7b26\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\uff0817+\u4e2a\u5b57\u8282\uff09\uff0c\u867d\u7136\u6ca1\u6709\u8d85\u65f6\uff0c\u4f46\u5b9e\u9645\u5de5\u7a0b\u4e2d\u662f\u4e0d\u80fd\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u3002<\/p>\n\n\n\n<pre lang=\"c++\">\n\/\/ https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2296-design-a-text-editor\/\nclass TextEditor {\npublic:\n  TextEditor(): data_{'$'}, cursor_{begin(data_)} {}\n  \n  void addText(string text) {\n    for (char c : text)\n      cursor_ = data_.insert(++cursor_, c);\n  }\n  \n  int deleteText(int k) {\n    for (int i = 0; i < k; ++i) {\n      if (cursor_ == begin(data_)) return i;\n      data_.erase(cursor_--);\n    }\n    return k;\n  }\n  \n  string cursorLeft(int k) {\n    for (int i = 0; i < k; ++i) {\n      if (cursor_ == begin(data_)) break;\n      cursor_--;\n    }\n    return getText();\n  }\n  \n  string cursorRight(int k) {\n    for (int i = 0; i < k; ++i) {\n      if (cursor_ == prev(end(data_))) break;\n      cursor_++;\n    }\n    return getText();\n  }\nprivate:\n  string getText() {\n    string ans;\n    auto it = cursor_;\n    for (int i = 0; i < 10; ++i) {\n      if (it == begin(data_)) break;\n      ans += *it--;\n    }\n    reverse(begin(ans), end(ans));\n    return ans;\n  }\n\n  list<char> data_;\n  list<char>::iterator cursor_;\n};\n<\/pre>\n\n\n\n<p>\u65b9\u6cd52: \u7528\u4e24\u4e2astring\u6765\u4ee3\u8868\u5149\u6807\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\u548c\u5149\u6807\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\u3002\u6ce8\uff1a\u53f3\u8fb9\u5b57\u7b26\u4e32\u662f\u53cd\u8f6c\u7684\u3002<\/p>\n\n\n\n<p>\u5de6\u53f3\u4e24\u8fb9\u7684\u5b57\u7b26\u4e32\u53ea\u4f1a\u589e\u957f\uff08\u6216\u8005\u8986\u76d6\uff09\uff0c\u4e0d\u4f1a\u7f29\u77ed\uff0c\u8fd9\u662f\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u5220\u9664\u7684\u65f6\u5019\u5c31\u662f\u4fee\u6539\u4e86\u957f\u5ea6\u800c\u5df2\uff0c\u5e76\u6ca1\u6709\u7f29\u77ed\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u662fO(1)\u7684\u3002<br>\u5982\u679c\u7f29\u77ed\u7684\u8bdd\u9700\u5219\u8981O(k)\u65f6\u95f4\uff0c\u8fd8\u8981\u52a0\u4e0a\u5185\u5b58\u91ca\u653e\/\u518d\u5206\u914d\u7684\u65f6\u95f4\uff0c\u5e94\u8be5\u4f1a\u6162\u4e00\u4e9b\u4f46\u4e0d\u591a\u3002<\/p>\n\n\n\n<p>\u79fb\u52a8\u5149\u6807\u7684\u65f6\u5019\uff0c\u5c31\u662f\u628a\u5de6\u8fb9\u5b57\u7b26\u4e32\u7684\u6700\u540ek\u4e2acopy\u5230\u53f3\u8fb9\u7684\u6700\u540e\u9762\uff0c\u6216\u8005\u53cd\u8fc7\u6765\u3002\u540c\u6837\u6ca1\u6709\u7f29\u77ed\uff0c\u53ea\u4f1a\u53d8\u957f\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6: TextEditor O(1) addText O(|text|) deleteText O(1) cursorLeft O(k) cursorRight(k)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n)\uff0cn\u4e3a\u6784\u5efa\u7684\u6700\u957f\u7684\u5b57\u7b26\u4e32<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-03-at-9.37.22\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"511\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-03-at-9.37.22\u202fPM.png\" alt=\"\" class=\"wp-image-10262\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-03-at-9.37.22\u202fPM.png 676w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-03-at-9.37.22\u202fPM-300x227.png 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/a><\/figure>\n\n\n\n<pre lang=\"c++\">\n\/\/ https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2296-design-a-text-editor\/\nclass TextEditor {\npublic:\n  TextEditor() {}\n  \n  void addText(string_view text) {\n    ensureSize(l, m + text.size());\n    copy(begin(text), end(text), begin(l) + m);\n    m += text.size();\n  }\n  \n  int deleteText(int k) {\n    k = min(k, m);\n    m -= k;\n    return k;\n  }\n  \n  string cursorLeft(int k) {\n    ensureSize(r, n + k);\n    for (k = min(k, m); k > 0; --k)\n      r[n++] = l[--m];\n    return getText();\n  }\n  \n  string cursorRight(int k) {\n    ensureSize(l, m + k);\n    for (k = min(k, n); k > 0; --k)\n      l[m++] = r[--n];\n    return getText();\n  }\nprivate:\n  inline void ensureSize(string& s, int size) {\n    if (s.size() < size)\n      s.resize(size);\n  }\n\n  string getText() const {\n    return string(begin(l) + m - min(m, 10), begin(l) + m);\n  }\n\n  string l;\n  string r;\n  int m = 0;\n  int n = 0;\n};\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd51\uff1a\u53cc\u5411\u94fe\u8868\u6765\u5b58\u50a8\u6587\u672c\uff0c\u8fed\u4ee3\u5668\u6307\u5411\u5149\u6807\u6240\u5728\u7684\u4f4d\u7f6e\u3002\u52a0\u4e00\u4e2adummy head\u4f1a\u4f7f\u4ee3\u7801\u7b80\u5355\u5f88\u591a\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aTextEditor O(1) addText O(|text|) deleteText O(k) cursorLeft O(k) cursorRight(k)\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n) \u7531\u4e8e\u6bcf\u4e2a\u5b57\u7b26\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\uff0817+\u4e2a\u5b57\u8282\uff09\uff0c\u867d\u7136\u6ca1\u6709\u8d85\u65f6\uff0c\u4f46\u5b9e\u9645\u5de5\u7a0b\u4e2d\u662f\u4e0d\u80fd\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u3002 \/\/ https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2296-design-a-text-editor\/ class TextEditor { public: TextEditor(): data_{&#8216;$&#8217;}, cursor_{begin(data_)} {} void addText(string text)&#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":[625,83,4],"class_list":["post-10252","post","type-post","status-publish","format-standard","hentry","category-string","tag-iterator","tag-list","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10252","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=10252"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10252\/revisions"}],"predecessor-version":[{"id":10265,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10252\/revisions\/10265"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}