OneCoder Avatar
OneCodercoderli.com · 1023 篇博文
{ } Java

Java 服务端架构

🎨 视觉封面

LeetCode Remove Element

📅 2017-10-26·✍️ onecoder·计算中...·⏱️ 6 分钟

Problem

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example: Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

跟从排好序的队列出移除重复元素问题类似。从给定的队列中移除给定的元素。最后,都需要把剩余的有效元素放在队列的开头,可以理解为同一个问题。

Java 实现

Java50 行

package com.coderli.leetcode.algorithms.easy;

/**
 * Given an array and a value, remove all instances of that value in place and return the new length.
 * <p>
 * Do not allocate extra space for another array, you must do this in place with constant memory.
 * <p>
 * The order of elements can be changed. It doesn't matter what you leave beyond the new length.
 * <p>
 * Example:
 * Given input array nums = [3,2,2,3], val = 3
 * <p>
 * Your function should return length = 2, with the first two elements of nums being 2.
 *
 * @author li.hzh 2017-10-26
 */
public class RemoveElement {

    public static void main(String[] args) {
        RemoveElement removeElement = new RemoveElement();
        System.out.println(removeElement.removeElement(new int[]{}, 3));
        System.out.println(removeElement.removeElement(new int[]{2, 3, 3, 2}, 3));
        System.out.println(removeElement.removeElementByAddCount(new int[]{2, 3, 3, 2, 4, 1}, 3));
    }

    public int removeElement(int[] nums, int val) {
        int result = nums.length;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] == val) {
                result--;
                int temp = nums[result];
                nums[result] = val;
                nums[i] = temp;
            }
        }
        return result;
    }

    public int removeElementByAddCount(int[] nums, int val) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[result++] = nums[i];
            }
        }
        return result;
    }
}

分析

这里给出两个解法,removeElementByAddCount,是从前往后遍历,跟remove duplicates from sorted array/ 问题解法思路一致了。

removeElement 解法,则是从后往前遍历,遇到给定值的元素,交换一下位置即可。

看完题解去实战!OneCoder C++ 信奥远征闯关平台 (oi.coderli.com)
💡 OneCoder 资源指引

所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI

🤝 技术交流与答疑

欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。

📚

猜你想读 · 相关文章推荐

OneCoder

OneCoder

一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com

读者讨论与留言

共 0 条讨论
邮箱必填 · 支持 Markdown 格式与直接回复
💬 还没有读者留言,快来成为第一个讨论者吧!