https://github.com/dolphinmind/datastructure/tree/datastructure-array
主类
package com.github.dolphinmind.array.binarysearch;
/**
* @author dolphinmind
* @ClassName RemoveElement
* @description 27 移除元素
* 移除元素分析
* 快慢指针
* 双向指针
* slow和left指针都处于有效数组的末尾,即不包含var的元素的最后一个位置之后,返回的slow和left即有效数组的长度
* @date 2024/7/31
*/
public class RemoveElement {
private int left;
private int right;
/**
* 快慢指针同向
* slow 处于有效数组的末尾,即不包含target的元素的最后一个位置之后,返回的left即有效数组的长度
*
* @param nums
* @param val
* @return
*/
public int removeElementTwoPointersCoextensive(int[] nums, int val) {
int slow = 0;
int fast = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
/**
* 双指针反向:左指针 右指针 while边界问题也符合开闭原则:左闭右开
* 疑惑:双指针如果left==right的时候,nums[left] == val,这时返回
* left 处于有效数组的末尾,即不包含target的元素的最后一个位置之后,返回的left即有效数组的长度
*
* @param nums
* @param val
* @return
*/
public int removeElementTowPointersReverse1(int[] nums, int val) {
left = 0;
right = nums.length - 1;
while (left <= right) {
if (nums[left] == val) {
nums[left] = nums[right];
right--;
} else {
left++;
}
}
if (nums[left] == val) {
return left - 1;
}
return left;
}
/**
* 左闭右闭
* @param nums
* @param val
* @return
*/
public int removeElementTowPointersReverse2(int[] nums, int val) {
left = 0;
right = nums.length;
while (left < right) {
if (nums[left] == val) {
nums[left] = nums[right - 1];
right--;
} else {
left++;
}
}
return left;
}
}
测试类
package com.github.dolphinmind.array.binarusearch;
import com.github.dolphinmind.array.binarysearch.RemoveElement;
import org.junit.Test;
/**
* @author dolphinmind
* @ClassName RemoveElementTest
* @description
* @date 2024/7/31
*/
public class RemoveElementTest {
/**
* 测试同向快慢双指针
*/
@Test
public void test_removeElementTwoPointersCoextensive() {
int[] nums = {3,2,2,3};
int val = 3;
RemoveElement removeElement = new RemoveElement();
int result = removeElement.removeElementTwoPointersCoextensive(nums, val);
System.out.println(result);
print(nums, result);
}
/**
* 测试反向双指针
*/
@Test
public void test_removeElementTowPointersReverse1() {
int[] nums = {0,1,2,2,3,0,4,2};
int val = 2;
RemoveElement removeElement = new RemoveElement();
int result = removeElement.removeElementTowPointersReverse1(nums, val);
System.out.println(result);
print(nums, result);
}
/**
* 测试反向双指针
*/
@Test
public void test_removeElementTowPointersReverse2() {
int[] nums = {};
int val = 2;
RemoveElement removeElement = new RemoveElement();
int result = removeElement.removeElementTowPointersReverse2(nums, val);
System.out.println(result);
print(nums, result);
}
public void print(int[] nums, int len) {
System.out.print("[");
for (int i = 0; i < len; i++) {
System.out.print(nums[i] + " ");
}
System.out.println("]");
}
}
标签:27,val,nums,int,RemoveElement,result,LeetCode,left
From: https://www.cnblogs.com/dolphinmind/p/18335553