JAVA solution--clear and easy


  • -3
    J
    public class Solution {
        public int singleNumber(int[] nums) {
            Arrays.sort(nums);
            if(nums.length==1) return nums[0];
            if(nums.length>1){
                if(nums[0]!=nums[1])return nums[0];
                if(nums[nums.length-1]!=nums[nums.length-2])return nums[nums.length-1];
                else{
                    for(int i=3;i<nums.length-1;i++){
                        if(nums[i]!=nums[i-1] && nums[i]!=nums[i+1]){
                            return nums[i];
                        }//if
                    }//for
                }//else
            }//if
            return nums[0];
        }
    }

  • -1
    P

    public class Solution {

    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        int i=0;
        for(; i<nums.length-2;) {
            if(nums[i] == nums[i+1] && nums[i]==nums[i+2])
                i+=3;
            else
                return nums[i];
        }
        return nums[i];
    }
    

    }


  • 0
    C

    simple but not effective


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.