g0001_0100.s0027_remove_element.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0027_remove_element;
// #Easy #Array #Two_Pointers #2023_08_09_Time_0_ms_(100.00%)_Space_40.9_MB_(87.68%)
public class Solution {
public int removeElement(int[] nums, int val) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
int j = len - 1;
int occurTimes = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == val) {
occurTimes++;
if (j == i) {
return len - occurTimes;
}
while (nums[j] == val) {
j--;
occurTimes++;
if (j == i) {
return len - occurTimes;
}
}
nums[i] = nums[j];
j--;
}
if (i == j) {
return len - occurTimes;
}
}
return len - occurTimes;
}
}