首页 > 其他 > 详细

[leetcode]645. Set Mismatch

时间:2018-01-25 21:31:08      阅读:256      评论:0      收藏:0      [点我收藏+]

两种方法

public int[] findErrorNums(int[] nums) {
        int[] res = new int[2];
        Set<Integer> set = new HashSet<>();
        int sum = 0;
        int l = nums.length;
        int target = 0;
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i]))
                target = i;
            set.add(nums[i]);
            sum+=nums[i];
        }
        res[0] = nums[target];
        res[1] = (l+1)*l/2-sum+nums[target];
        return res;
    }
    public int[] findErrorNums2(int[] nums) {
        int[] res = new int[2];
        for (int a :
                nums) {
            if (nums[Math.abs(a)-1] >0) nums[Math.abs(a)-1] *=-1;
            else
            {
                res[0] = Math.abs(a);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>0)
            {
                res[1] = i+1;
            }
        }
        return res;
    }

 

[leetcode]645. Set Mismatch

原文:https://www.cnblogs.com/stAr-1/p/8353029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!