Single Number II

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?

1 自己的代码
package leetcode;

import java.util.Arrays;

public class SingleNumberII {
	public int singleNumber(int[] A) {
		Arrays.sort(A);
		int index = 0;
		for(int i = 0; i < A.length; i = i + 3){
			if(i == A.length-1) index = i;//处理数组中只有一个、处理singleNumber在数组最后的情况
			else{
				if(A[i] == A[i+1]) continue;
				else {index = i; break;}
			}
		}
        return A[index];
    }
	}

你可能感兴趣的:(number)