关闭

Single Number

标签: java
37人阅读 评论(1) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1131次
    • 积分:222
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论