前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
public class Solution {
public int singleNumber(int[] nums) {
if(nums.length == 1) return nums[0];
Arrays.sort(nums);
int value = nums[nums.length -1];
for(int i = 0; i < nums.length - 2; i += 3){
if(nums[i] != nums[i+1]){value = nums[i];break;}
}
return value;
}
}