class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
//for循环nums遍历
for(int num : nums){
//num-1得到的是List的对应的key,(num-1)%n得到的是key对应的值
int x = (num - 1) % n;
//结果肯定>n
nums[x] += n;
}
//ArrayList是一个泛型容器,新建ArrayList需要实例化泛型参数
//add方法添加元素到末尾 ret.add("老马");
//判断是否为空 public boolean isEmpty()
//获取长度 public int size()
//访问指定位置的元素 public E get(int index)
//查找元素 public int indexOf(Object o) 如果找到,返回索引位置,否则返回-1。
//从后往前找 public int lastIndexOf(Object o)
//是否包含指定元素 public boolean contains(Object o) 相同的依据是equals方法返回true。如果传入的元素为null,则找null的元素。
//删除指定位置的元素 public E remove(int index) 返回值为被删对象。
//删除指定对象 public boolean remove(Object o)
//删除所有元素 public void clear()
//在指定位置插入元素 public void add(int index, E element) index为0表示插入最前面,index为ArrayList的长度表示插到最后面
//修改指定位置的元素内容 public E set(int index, E element)
//新建一个List ret用来装不在nums的数字
List<Integer> ret = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
//如果num[i]<=n,说明不在nums里,所以加进ret,最后输出ret
if(nums[i] <= n){
ret.add(i+1);
}
}
return ret;
}
}
leetcode hot100------448. 找到所有数组中消失的数字
最新推荐文章于 2024-08-03 21:02:21 发布