Single Number II - LeetCode

来源:互联网 发布:伊藤润二的漫画 知乎 编辑:程序博客网 时间:2024/05/16 18:55

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];    }}


0 0
原创粉丝点击