您好,登錄后才能下訂單哦!
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3]
, val = 3
Your function should return length = 2, with the first two elements of nums being 2.
題目大意:刪除容器中指定的重復(fù)元素,然后返回容器的長度。要求不能申請(qǐng)數(shù)組來處理。
class Solution { public: int removeElement(vector<int>& nums, int val) { for(int i = 0; i < nums.size(); i++) { if(nums[i] == val ) { nums.erase (nums.begin() + i ); i--; } } return nums.size(); } };
2016-08-05 20:42:00
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。