# 0027. Remove Element ###### tags: `Leetcode` `双指针` Link: https://leetcode.com/problems/remove-element/ 历史性的辉煌时刻,十分钟解一题 ## Code ```c class Solution { public: int removeElement(vector<int>& nums, int val) { int i = 0; for(int j = 0;j < nums.size();j++){ if(nums[j]!=val){ nums[i] = nums[j]; i++; } } return i; } }; ``` ## Code in Java # 历史的黑暗时刻,一小时没做出来 ```java class Solution { public int removeElement(int[] nums, int val) { // if (nums.length == 0) return 0; int j = 0; for (int i = 0; i < nums.length; i++){ if (nums[i] != val){ if (i > j) nums[j] = nums[i]; j++; } } return j; } } ```
×
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