27. 移除元素

题目链接:https://leetcode-cn.com/problems/remove-element/

执行用时:1 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:36.7 MB, 在所有 Java 提交中击败了96.64% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int removeElement(int[] nums, int val) {
int count=0;
int temp;
int right = nums.length-1;
Arrays.sort(nums);
for (int i=0; i< nums.length; ++i){
if(nums[i] !=val){
count++;
}else if(i<right){
temp = nums[i];
if (nums[right]!=temp){
nums[i] = nums[right];
nums[right] = temp;
count++;
}
right--;
}
}
return count;
}
}

27. 移除元素
https://pisces34.github.io/2021/06/20/leetcode/removeElement/
发布于
2021年6月20日
许可协议