题解 | #数组中只出现一次的两个数字#
数组中只出现一次的两个数字
https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public List<int> FindNumsAppearOnce (List<int> nums) { // write code here for (int i = 0; i < nums.Count; i++) { int nF = nums.FindIndex(r => r == nums[i]); int nL = nums.FindLastIndex(r => r == nums[i]); if (nF != nL) { nums[nL] = nums[nF] = 0; } } List<int> ls = new List<int>(); for (int i = 0; i < nums.Count; i++) { if (nums[i] == 0) continue; ls.Add(nums[i]); } return ls[0] > ls[1] ? new List<int>(2){ ls[1], ls[0] } : ls; } }