【Leetcode】448. Find All Numbers Disappeared in an Array

思路: 用标志位的方法去做,把原数组中出现的数应该在的位置上的数置为负值,然后重新遍历如果大于0,说明未置为负值过,也就是表示从未出现过。 public class Solution {    public Lis...

思路:


用标志位的方法去做,把原数组中出现的数应该在的位置上的数置为负值,然后重新遍历如果大于0,说明未置为负值过,也就是表示从未出现过。


public class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<Integer>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            int index = Math.abs(nums[i]) - 1;
            if (nums[index] > 0)
                nums[index] = -nums[index];
        }
        for (int i = 0; i < len; i++) {
            if (nums[i] > 0)
                result.add(i + 1);
        }
        return result;
    }
}

Runtime:18ms

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
廖雪
廖雪

78 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章