Leetcodes Solutinos 26 Remove Duplicates from Sorted Array
匯總
雪之下雪乃:leetcode解題總匯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 by modifying the input array in-place with O(1) extra memory.
Example:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesnt matter what you leave beyond the new length.
思路1
只把不相等的元素用id存到數組的最前面
class Solution {public: int removeDuplicates(vector<int>& nums){ int n = nums.size(); if(n < 2) return n; int id = 1; for(int i = 1; i < n; ++i) if(nums[i] != nums[i-1]) nums[id++] = nums[i]; return id; }};
推薦閱讀:
※ACM演算法之博弈
※你是不是特別苦逼的到處找計算機資料?別愁了!學習筆記+視頻教程+項目源碼+配套工具免費送
※Arduino UNO 俄羅斯方塊
※個人感想:《On Lisp》和 《DSL》:lisp為什麼不流行?