leetcode 442.数组中重复的数据

题目: 找到数组中重复的数字。

class Solution
{
    
    
    public List<Integer> findDuplicates(int[] nums)
    {
    
       
      Set<Integer> set = new HashSet();
      List<Integer> list= new ArrayList(); 
      for(int i:nums)
      {
    
       
          if(!set.contains(i))
          set.add(i);
          else
          {
    
    
              list.add(i);
          }
      }
        return list;
    }
}

建立hashset,遍历数组插入,已存在则加入list,最终返回list;

猜你喜欢

转载自blog.csdn.net/qq_45789385/article/details/102744142
今日推荐