有一个 leetCode problem提示如下
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:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
这个问题在 C#(网站上解决这个问题的受支持语言之一)中有意义吗?不能修改数组的长度,只能分配一个新的数组。
最佳答案
这个问题在 C# 中有意义吗? 是的。
[在C#中]不能修改数组的长度,只能分配一个新的数组。 绝对正确。所以重新阅读问题,看看你能做什么。我不想给出答案,因为很明显你会从自己解决这个问题中学到东西。所以在这里停止阅读;如果您需要提示,请继续阅读。
提示:关注这部分...
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
关于c# - 在 C# 中修改数组 "in-place"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789318/