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[] A) {
Arrays.sort(A);
int count = 0;
int i = 0;
for(i = 0; i < A.length; i++){
if(count == 0){
count ++;
}
else if(count == 1){
if(A[i] != A[i - 1])
return A[i-1];
else{
count ++;
}
}
else if(count == 2){
if(A[i] != A[i - 1])
return A[i-1];
else{
count = 0;
}
}
}
return A[A.length - 1];
}
}
本文介绍了一种算法,该算法能在整数数组中找到那个仅出现一次的元素,而其他所有元素都出现了三次。文章提供的解决方案确保了线性的运行时间复杂度,并探讨了如何在不使用额外内存的情况下实现这一目标。
246

被折叠的 条评论
为什么被折叠?



