zoukankan      html  css  js  c++  java
  • LeetCode Contains Duplicate (判断重复元素)

    题意:

      如果所给序列的元素不是唯一的,则返回true,否则false。

    思路:

      哈希map解决。

     1 class Solution {
     2 public:
     3     bool containsDuplicate(vector<int>& nums) {
     4         unordered_map<int,int> mapp;
     5         for(int i=0; i<nums.size(); i++)
     6         {
     7             if(mapp[nums[i]])   return true;
     8             else    mapp[nums[i]]=1;
     9         }
    10         return false;
    11     }
    12 };
    AC代码

    python3

    直接排序,再比对相邻元素

     1 class Solution(object):
     2     def containsDuplicate(self, nums):
     3         """
     4         :type nums: List[int]
     5         :rtype: bool
     6         """
     7         if nums==[]:    return False
     8         nums=sorted(nums)
     9         i=1
    10         while i<len(nums):
    11             if nums[i-1]==nums[i]:
    12                 return True
    13             i+=1
    14         return False
    AC代码

    用set辅助

    1 class Solution(object):
    2     def containsDuplicate(self, nums):
    3         """
    4         :type nums: List[int]
    5         :rtype: bool
    6         """
    7         return len(set(nums))<len(nums)
    AC代码
  • 相关阅读:
    cf C. Vasya and Robot
    zoj 3805 Machine
    cf B. Vasya and Public Transport
    cf D. Queue
    cf C. Find Maximum
    cf B. Two Heaps
    cf C. Jeff and Rounding
    cf B. Jeff and Periods
    cf A. Jeff and Digits
    I Think I Need a Houseboat
  • 原文地址:https://www.cnblogs.com/xcw0754/p/4621502.html
Copyright © 2011-2022 走看看