From 8f17821712c6c6f9e08ec5fe850dc45ae9053321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=A0=91=E6=A3=AE?= <14021051@buaa.edu.cn> Date: Sat, 1 Sep 2018 18:54:03 +0800 Subject: [PATCH] Create 027-Remove Element.md --- 027-Remove Element.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 027-Remove Element.md diff --git a/027-Remove Element.md b/027-Remove Element.md new file mode 100644 index 0000000..bb56161 --- /dev/null +++ b/027-Remove Element.md @@ -0,0 +1,15 @@ +# 思路 +类似第26题, 用count记录非val的个数,从前往后遍历,如果值为val则跳过,否则令nums[count并=nums[i]并自增count +# C++ +``` +class Solution { +public: + int removeElement(vector& nums, int val) { + int count = 0; + for(int i = 0; i < nums.size(); i++){ + if(nums[i] != val) nums[count++] = nums[i]; + } + return count; + } +}; +```