Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Subscribe to see which companies asked this question

我的方法貌似用的是O(n*n)的时间复杂度的。。不过第一次脱离IDE,白板编程~还是有待于提高的~~

public class Solution {

    public int singleNumber(int[] nums) {
        int[] arr = new int[nums.length];
        int r = 0;
        for(int i = 0 ;i < nums.length ; i++)
        arr[i] = 0;
        for(int i = 0; i < nums.length ; i++){
            for(int j = i+1 ; j <nums.length ; j++)
            {
                if(nums[i] == nums[j])
                {
                    arr[i]=1;
                    arr[j]=1;
                }
            }
        }
        for(int i = 0 ; i < nums.length ;i++){
            if(arr[i]==0)
            r = i;
        }
        return nums[r];
    }
}
阅读更多
文章标签: java
个人分类: LeetCode Java
上一篇Nim Game
下一篇Add Digits
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭