{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyOYQMaKnPTttORNM3cYKBx4", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Removing an Element\n", "\n", "## Problem\n", "\n", "Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.\n", "\n", "Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:\n", "\n", "Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.\n", "Return k.\n", "\n", "```\n", "Custom Judge:\n", "\n", "The judge will test your solution with the following code:\n", "int[] nums = [...]; // Input array\n", "int val = ...; // Value to remove\n", "int[] expectedNums = [...]; // The expected answer with correct length.\n", " // It is sorted with no values equaling val.\n", "int k = removeElement(nums, val); // Calls your implementation\n", "assert k == expectedNums.length;\n", "sort(nums, 0, k); // Sort the first k elements of nums\n", "for (int i = 0; i < actualLength; i++) {\n", " assert nums[i] == expectedNums[i];\n", "}\n", "\n", "If all assertions pass, then your solution will be accepted.\n", "Example 1:\n", "Input: nums = [3,2,2,3], val = 3\n", "Output: 2, nums = [2,2,_,_]\n", "Explanation: Your function should return k = 2, with the first two elements of nums being 2.\n", "It does not matter what you leave beyond the returned k (hence they are underscores).\n", "Example 2:\n", "Input: nums = [0,1,2,2,3,0,4,2], val = 2\n", "Output: 5, nums = [0,1,4,0,3,_,_,_]\n", "Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.\n", "Note that the five elements can be returned in any order.\n", "It does not matter what you leave beyond the returned k (hence they are underscores).\n", "Constraints:\n", "0 <= nums.length <= 100\n", "0 <= nums[i] <= 50\n", "0 <= val <= 100\n", "\n", "```\n", "\n", "## Solution\n" ], "metadata": { "id": "VXIFNA8N5w_r" } }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OGhBr5xF5l6h", "outputId": "9807ea09-e7d8-4f34-b32c-2eb5b3cb7e4f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2\n", "[2, 2]\n", "5\n", "[0, 1, 3, 0, 4]\n" ] } ], "source": [ "from typing import List\n", "\n", "class Solution:\n", " def removeElement(self, nums: List[int], val: int) -> int:\n", " # Initialize two pointers, one for iterating the list and one for the insertion point.\n", " i = 0\n", " k = 0\n", "\n", " while i < len(nums):\n", " if nums[i] != val:\n", " # If the current element is not equal to val, copy it to the insertion point.\n", " nums[k] = nums[i]\n", " k += 1\n", " i += 1\n", "\n", " return k\n", "\n", "# Example usage and custom judge test\n", "if __name__ == \"__main__\":\n", " solution = Solution()\n", "\n", " # Test case 1\n", " nums1 = [3, 2, 2, 3]\n", " val1 = 3\n", " k1 = solution.removeElement(nums1, val1)\n", " print(k1) # Expected output: 2\n", " print(nums1[:k1]) # Expected output: [2, 2]\n", "\n", " # Test case 2\n", " nums2 = [0, 1, 2, 2, 3, 0, 4, 2]\n", " val2 = 2\n", " k2 = solution.removeElement(nums2, val2)\n", " print(k2) # Expected output: 5\n", " print(nums2[:k2]) # Expected output: [0, 1, 4, 0, 3]" ] } ] }