{"id":1890,"date":"2016-03-20T10:52:33","date_gmt":"2016-03-20T02:52:33","guid":{"rendered":"http:\/\/boweihe.me\/?p=1890"},"modified":"2016-03-20T10:52:33","modified_gmt":"2016-03-20T02:52:33","slug":"leetcode-26-remove-duplicates-from-sorted-array","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1890","title":{"rendered":"LeetCode 26. Remove Duplicates from Sorted Array"},"content":{"rendered":"<p>Given a sorted array, remove the duplicates in place such that each element appear only <i>once<\/i> and return the new length.<br \/>\nDo not allocate extra space for another array, you must do this in place with constant memory.<br \/>\nFor example,<br \/>\nGiven input array <i>nums<\/i> = <code>[1,1,2]<\/code>,<br \/>\nYour function should return length = <code>2<\/code>, with the first two elements of <i>nums<\/i> being <code>1<\/code> and <code>2<\/code> respectively. It doesn&#8217;t matter what you leave beyond the new length.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u5006\u6307\u9488~\u4e00\u4e2a\u6307\u5411\u5f53\u524d\u5b9e\u9645\u6709\u6548\u7684\u4f4d\u7f6e\uff0c\u4e00\u4e2a\u6307\u5411\u5f53\u524d\u5904\u7406\u7684\u4f4d\u7f6e<br \/>\n<em>\u9898\u5916\u8bdd\uff1a\u7ecf\u5386\u611f\u5192\u9f3b\u585e\u5589\u5499\u75db\u5feb\u4e00\u5468\uff0c\u8111\u5b50\u7ec8\u4e8e\u662f\u6e05\u695a\u4e86\uff01<\/em><\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    int removeDuplicates(vector&lt;int&gt;&amp; nums) {\n        int numSz = nums.size();\n        if(numSz &lt;= 1)\n            return numSz;\n        int i=0;\n        int j=1;\n        while(j &lt; numSz){\n            if(nums[j] == nums[i])\n                j++;\n            else {\n                nums[++i] = nums[j];\n                j++;\n            }\n        }\n        return i+1;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2], Your function should return length = 2, with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[64,66],"class_list":["post-1890","post","type-post","status-publish","format-standard","hentry","category-study","tag-leet","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1890"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1890\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}