{"id":2040,"date":"2022-05-20T17:29:27","date_gmt":"2022-05-20T17:29:27","guid":{"rendered":"https:\/\/www.zttofficial.com\/?p=2040"},"modified":"2022-05-20T17:32:46","modified_gmt":"2022-05-20T17:32:46","slug":"%e5%81%9a%e9%a1%8c%e7%ad%86%e8%a8%98%ef%bc%9aremove-duplicates-from-sorted-array-java","status":"publish","type":"post","link":"https:\/\/www.zttofficial.com\/?p=2040","title":{"rendered":"\u505a\u984c\u7b46\u8a18\uff1aRemove Duplicates from Sorted Array (Java)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4eca\u5f8c Easy \u984c\u82e5\u6c92\u78b0\u5230\u975e\u5e38\u7cbe\u5f69\u7684\u5c31\u4e0d\u6bcf\u984c\u90fd\u66f4\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u984c\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7d66\u5b9a\u4e00\u500b\u6309\u975e\u905e\u6e1b\u9806\u5e8f\u6392\u5e8f\u7684\u6574\u6578\u6578\u7d44 <kbd>nums<\/kbd>\uff0c\u5c31\u5730\u522a\u9664\u91cd\u8907\u9805\uff0c\u4f7f\u6bcf\u500b\u55ae\u7368\u7684\u5143\u7d20\u53ea\u51fa\u73fe\u4e00\u6b21\u3002\u5143\u7d20\u7684\u76f8\u5c0d\u9806\u5e8f\u61c9\u4fdd\u6301\u4e0d\u8b8a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u65bc\u5728\u67d0\u4e9b\u8a9e\u8a00\u4e2d\u7121\u6cd5\u66f4\u6539\u6578\u7d44\u7684\u9577\u5ea6\uff0c\u56e0\u6b64\u60a8\u5fc5\u9808\u5c07\u7d50\u679c\u653e\u5728\u6578\u7d44 <kbd>nums<\/kbd> \u7684\u524d\u534a\u90e8\u5206\u3002\u66f4\u6b63\u5f0f\u5730\u8aaa\uff0c\u5982\u679c\u522a\u9664\u91cd\u8907\u9805\u5f8c\u6709 <kbd>k<\/kbd> \u500b\u5143\u7d20\uff0c\u5247 <kbd>nums<\/kbd> \u7684\u524d k \u500b\u5143\u7d20\u61c9\u6301\u6709\u6700\u7d42\u7d50\u679c\u3002\u9664\u4e86\u524d <kbd>k<\/kbd> \u500b\u5143\u7d20\u4e4b\u5916\uff0c\u7559\u4e0b\u4ec0\u9ebc\u90fd\u4e0d\u91cd\u8981\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c07\u6700\u7d42\u7d50\u679c\u653e\u5165 <kbd>nums<\/kbd> \u7684\u524d <kbd>k<\/kbd> \u500b\u63d2\u69fd\u5f8c\u8fd4\u56de <kbd>k<\/kbd>\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: nums = [1,1,2]\nOutput: 2, nums = [1,2,_]\nExplanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.\nIt does not matter what you leave beyond the returned k (hence they are underscores).<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: nums = [0,0,1,1,1,2,2,3,3,4]\nOutput: 5, nums = [0,1,2,3,4,_,_,_,_,_]\nExplanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.\nIt does not matter what you leave beyond the returned k (hence they are underscores).<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Stack \u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u5df1\u9084\u662f\u53ea\u80fd\u60f3\u51fa\u4f86\u8822\u65b9\u6cd5\uff0c\u61c9\u8a72\u5237\u984c\u91cf\u9084\u9060\u9060\u4e0d\u5920\u5427\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">class Solution {\n    public int removeDuplicates(int[] nums) {\n\t\tStack&lt;Integer&gt; Stack = new Stack&lt;Integer&gt;();\n\t\tint k = 0;\n\t\tfor (int i = 0; i &lt; nums.length; i++) {\n\t\t\tif (Stack.search(nums[i])==-1) {\n\t\t\t\tStack.push(nums[i]);\n\t\t\t\tnums[k] = nums[i];\n\t\t\t\tk++;\n\t\t\t} \n\t\t}\n\t\treturn k;\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">7 ms<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5229\u7528\u9806\u5e8f\u7684\u53d6\u5de7\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">class Solution {\n    public int removeDuplicates(int[] nums) {\n        int i = 0;\n        for(int n: nums){\n            if(i == 0 || n &gt; nums[i - 1]){\n                nums[i] = n;\n                i++;\n            }\n        }\n        return i;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">1 ms<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5f8c Easy \u984c\u82e5\u6c92\u78b0\u5230\u975e\u5e38\u7cbe\u5f69\u7684\u5c31\u4e0d\u6bcf\u984c\u90fd\u66f4\u4e86\u3002 \u984c\u76ee\u63cf\u8ff0\uff1a \u7d66\u5b9a\u4e00\u500b\u6309\u975e\u905e\u6e1b\u9806\u5e8f\u6392\u5e8f\u7684\u6574\u6578\u6578\u7d44 nums\uff0c\u5c31\u5730\u522a\u9664\u91cd\u8907\u9805\uff0c\u4f7f\u6bcf\u500b\u55ae\u7368\u7684\u5143\u7d20\u53ea\u51fa\u73fe\u4e00\u6b21\u3002\u5143\u7d20\u7684\u76f8\u5c0d\u9806\u5e8f\u61c9\u4fdd\u6301\u4e0d\u8b8a\u3002 \u7531\u65bc\u5728\u67d0\u4e9b\u8a9e\u8a00\u4e2d\u7121\u6cd5\u66f4\u6539\u6578\u7d44\u7684\u9577\u5ea6\uff0c\u56e0\u6b64\u60a8\u5fc5\u9808\u5c07\u7d50\u679c\u653e\u5728\u6578\u7d44 nums \u7684\u524d\u534a\u90e8\u5206\u3002\u66f4\u6b63\u5f0f\u5730\u8aaa\uff0c\u5982\u679c\u522a\u9664\u91cd\u8907\u9805\u5f8c\u6709 k \u500b\u5143\u7d20\uff0c\u5247 nums \u7684\u524d k \u500b\u5143\u7d20\u61c9\u6301\u6709\u6700\u7d42\u7d50\u679c\u3002\u9664\u4e86\u524d k \u500b\u5143\u7d20\u4e4b\u5916\uff0c\u7559\u4e0b\u4ec0\u9ebc\u90fd\u4e0d\u91cd\u8981\u3002 \u5c07\u6700\u7d42\u7d50\u679c\u653e\u5165 nums \u7684\u524d k \u500b\u63d2\u69fd\u5f8c\u8fd4\u56de k\u503c\u3002 Example 1: Example 2: Stack \u89e3\u6cd5\uff1a \u81ea\u5df1\u9084\u662f\u53ea\u80fd\u60f3\u51fa\u4f86\u8822\u65b9\u6cd5\uff0c\u61c9\u8a72\u5237\u984c\u91cf\u9084\u9060\u9060\u4e0d\u5920\u5427\uff1a 7 ms \u5229\u7528\u9806\u5e8f\u7684\u53d6\u5de7\u65b9\u6cd5\uff1a 1 ms<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,135],"tags":[137,140],"class_list":["post-2040","post","type-post","status-publish","format-standard","hentry","category-all","category-135","tag-leetcode","tag-stack"],"_links":{"self":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2040"}],"version-history":[{"count":2,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2040\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/2040\/revisions\/2042"}],"wp:attachment":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}