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 #2022_06_14_Time_0_ms_(100.00%)_Space_42.5_MB_(46.77%)
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;
}
}