Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Last active July 18, 2017 02:35
Show Gist options
  • Save BiruLyu/17547405dd4627c092b808f6ae1d5f2b to your computer and use it in GitHub Desktop.
Save BiruLyu/17547405dd4627c092b808f6ae1d5f2b to your computer and use it in GitHub Desktop.
public class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for(int j = 0; j < nums.length;j++){
if(nums[j]!=val){
nums[i]=nums[j];
i ++;
}
}
return i;
}
}
public class Solution {
public int removeElement(int[] nums, int val) {
if (nums.length == 0 || nums == null) return 0;
Arrays.sort(nums);
int count = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] < val) {
continue;
} else if (nums[i] == val) {
count++;
} else {
nums[i - count] = nums[i];
}
}
return nums.length - count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment