LeetcCode 27: remove elements Remove Element (python, java)

Public number: Write love bug

Given an array nums and a value val , you need to place to remove all values equal to val element returns the new length of the array after removal.

Do not use extra array of space, you have to modify the input array place and completed under the conditions of use O (1) extra space.

Order of the elements may be changed. You do not need to consider beyond the elements of the new array length behind.

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

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

Example 1:

给定 nums = [3,2,2,3], val = 3,

函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。

你不需要考虑数组中超出新长度后面的元素。

Example 2:

给定 nums = [0,1,2,2,3,0,4,2], val = 2,

函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。

注意这五个元素可为任意顺序。

你不需要考虑数组中超出新长度后面的元素。

Description:

Why return value is an integer, but the answer is an array of output it?

Please note that the input array is a "reference" transfer mode, which means to modify the input array is visible to the caller within the function.

You can imagine the internal operation is as follows:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

Problem-solving ideas:

Only allowed to modify the original array, can be two-hand, left pointer i from left to right, right to left and the right pointer j. If the index i and val are equal, the index i gets the value of the index j, and j forward one. If not equal, after i move one.

Because the required length and to return the modified considering only the length of the array, then do not consider the length of the array after, so just get the value of the index j, then the index j is no value to the value of index i.

Java:

class Solution {
    public int removeElement(int[] nums, int val) {
        int i=0,j=nums.length-1;//i-左指针;j-右指针
        while (i<=j){
            if(nums[i]==val){
                nums[i]=nums[j];//得到索引j的值,无需把索引j的值改为索引i的值
                j--;
            }else i++;
        }
        return j+1;
    }
}

Python3:

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        i=0
        j=len(nums)-1
        while i<=j:
            if(nums[i]==val):
                nums[i]=nums[j]
                j-=1
            else:i+=1
        return j+1

to sum up:

This question itself is very simple, just find out ideas together will become clear.

I love to write bug.png

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11120080.html