2021-04-30 14:58:46 +08:00

31 lines
869 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.fanxb.common;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA
* 移除元素
* 地址https://leetcode-cn.com/problems/remove-element/
* 思路:
* 暴力办法两层for循环每找到一个相同元素就将后面的所有元素向前移动一位
* 还有一种简便点的办法,不需要删除重复元素,只需要将重复元素替换为不重复元素即可
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
public class Q27 {
public int removeElement(int[] nums, int val) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[count++] = nums[i];
}
}
return count;
}
public static void main(String[] args) {
System.out.println(new Q27().removeElement(new int[]{0, 1, 2, 2, 3, 0, 4, 2}, 2));
}
}