27. Remove Element

来源:互联网 发布:卸载kingroot的软件 编辑:程序博客网 时间:2024/06/05 19:57

题目摘要
给定一个整数数组,删除数组中指定值并返回新数组长度

解法
双指针法,一个从前往后走,一个从后往前走,将前面的指定值替换为后面和指定值不相同的值

注意
直到前面指针代表的值和指定值不相等,前面的指针才向后移动一位;而后面的指针不断向前面移动

原题
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

0 0
原创粉丝点击