leetcode448 Find All Numbers Disappeared in an Array

注:此博客不再更新,所有最新文章将发表在个人独立博客limengting.site。分享技术,记录生活,欢迎大家关注

package array;

import java.util.ArrayList;

public class leecode448FindNumbersNotAppear {
    /*448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array),
some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime?
You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]
*/
    public static ArrayList<Integer> findNumbersNotAppear(int[] nums) {
     /*自己没想到,看的讨论区的:
     时间复杂度O(n),空间复杂度O(1)
     因为 1 <= 值 <= n, 0 <= 索引 <= n - 1
     * 第一遍遍历数组,n出现了就把第n位上的数取负号作为标记,再遍历一遍数组,正数所在的为第几位几就是没出现过的数
     * 自己觉得污染了原始数据,最好再把数组复原成原始数组*/
     if (nums == null || nums.length == 0) return null;
        ArrayList<Integer> res = new ArrayList<>();
        for (int num : nums) {
            int index = Math.abs(num) - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
            }
        }
        for (int i = 0; i < nums.length; i ++) {
            if (nums[i] > 0) {
                res.add(i + 1);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                nums[i] = -nums[i];
            }
        }
        /*迭代器只是取数,不能赋值
        for (int num : nums) {
            if (num < 0) {
                num = -num;
            }
        }*/
        return res;
    }
    public static void printArrayList(ArrayList<Integer> arrayList) {
        if (arrayList == null || arrayList.size() == 0) return;
        Integer[] array = arrayList.toArray(new Integer[arrayList.size()]);
        for (Integer i : array) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
    public static void printArray(int[] nums) {
        for (int i = 0; i < nums.length - 1; i ++) {
            System.out.print(nums[i] + " ");
        }
        System.out.println(nums[nums.length - 1]);
    }

    public static void main(String[] args) {
        int[] nums = new int[]{4,3,2,7,8,2,3,1};
        printArray(nums);
        printArrayList(findNumbersNotAppear(nums));
        printArray(nums);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值