-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRemoveElement_27.java
50 lines (42 loc) · 1.17 KB
/
RemoveElement_27.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.leetcode.problems.easy;
/**
* @author neeraj on 01/09/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class RemoveElement_27 {
public static void main(String[] args) {
System.out.println(removeElement(new int[]{3,2,2,3}, 3));
}
public static int removeElement(int[] nums, int val) {
int left = 0;
int right = 0;
int totalSwaps = 0;
// if(nums.length == 0)
// return 0;
//
// if(nums.length == 1 && nums[0] == val) {
// nums[0] = -val;
// return 0;
// }
while(left <= right && right < nums.length) {
if(nums[left] == val) {
if(nums[right] == val) {
totalSwaps++;
right++;
} else {
swap(nums, left++, right++);
}
} else {
left++;
right++;
}
}
return nums.length - totalSwaps;
}
public static void swap(int [] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}