[C#] Remove Element 解法

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,,]

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,,,_]

       static void Main(string[] args)       {           RemoveElement();       }       private static void RemoveElement()       {           var nums = new int[] { 3, 2, 2, 3,};           int val = 3;           int length = RemoveElement(nums, val);           Console.WriteLine($"新的长度:{length}");           Console.ReadKey();       }       private static int RemoveElement(int[] nums, int val)       {           int index = 0;           for (int i = 0; i < nums.Length; i++)           {               if (nums[i] != val)               {                   nums[index] = nums[i];                   index += 1;               }           }           return index;       }

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章