# leetcode解題:(Easy) 26. Remove Duplicates from Sorted Array 題目:[https://leetcode.com/problems/remove-duplicates-from-sorted-array/](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) 描述:給定一個遞增排序的陣列,調整陣列讓陣列前k個元素為陣列中出現的k個數字,回傳值為k。空間複雜度限制O(1),所有對陣列元素的操作只能在輸入的陣列中進行([in-place](https://zh.wikipedia.org/wiki/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)) 解題思路:用two pointer法,一個pointer用來找尋新的數字,一個pointer用來放置找到的數字 程式碼: ```JAVA= class Solution { public int removeDuplicates(int[] nums) { int l = 0; for(int r = 1; r < nums.length; r++) { if(nums[l] != nums[r]) { l++; nums[l] = nums[r]; } } return l+1; } } ``` 時間複雜度:O(n) 空間複雜度:O(1) ###### tags: `leetcode` `easy` `two pointer`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up