LintCode:落单的数 III

LintCode:落单的数 III

Python

class Solution:
    """ @param A : An integer array @return : Two integer """
    def singleNumberIII(self, A):
        # write your code here
        A = sorted(A)
        ans = []
        i = 0
        while i < len(A) - 1:
            if(A[i] == A[i+1]):
                i += 2
            else:
                ans.append(A[i])
                i += 1
        if A[len(A) - 1] != A[len(A)-2]:
            ans.append(A[len(A) - 1])
        return ans

Java

public class Solution {
    /** * @param A : An integer array * @return : Two integers */
    public List<Integer> singleNumberIII(int[] A) {
        // write your code here
        Arrays.sort(A);
        int m = 0;
        ArrayList ans = new ArrayList();
        while(m < A.length - 1){
            if(A[m] != A[m+1]){
                ans.add(A[m]);
                m += 1;
            }
            else{
                m += 2;
            }
        }
        if(A[A.length-1] != A[A.length-2]){
            ans.add(A[A.length-1]);
        }
        return ans;
    }
}

你可能感兴趣的:(LintCode:落单的数 III)