c# - 在 C# 中修改数组 "in-place"?

标签 c# arrays .net algorithm

有一个 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/

相关文章:

c# - Excel VSTO -> Hide/Unhide Ribbon Button based on another Ribbon Button 单击

c# - Entity Framework 性能问题

c# - 在代码文件中添加更多命名空间会影响性能吗?

c++ - 我想我可能想出了一个数组类型右值的例子

c# - 从 C# 代码从 SQLite 导入/导出 CSV

c# - mvvm light - 发送带回调的通知消息

c - 使用 qsort 对字符串数组进行排序

通过多个表单输入的 PHP 数组

.net - EasyNetQ - EasyNetQ.Topology.Queue

c# - .NET 图形数据库