题解 | #出现一次的牛#
出现一次的牛
https://www.nowcoder.com/practice/ed54a59697a244d2a3c656191d575f22
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int findSingleCow (int[] nums) { Arrays.sort(nums); int left = 0; int right = left + 2; int res = -1; if(nums[nums.length-1]!=nums[nums.length-2]) res = nums[nums.length-1]; else while(right<=nums.length-1){ if(nums[left]==nums[right]){ left = left + 3; right = left + 2; }else{ res = nums[left]; break; } } return res; } }