LeetCode刷题记录(1)—— 217. 存在重复元素

class Solution {
    //方法零:暴力搜索 (n^2) 超出时间限制
    /*public:
    bool containsDuplicate(vector<int>& nums) {
        for(int i=0;i<nums.size();i++){
            for(int j=i+1;j<nums.size();j++){
                if(nums[i]==nums[j]){
                    return true;
                }
            }
        }
        return false;
    }*/
    //方法一:遍历数组,将数组中每个数作为key存入map中,插入map前先查询map中是否已经含有该key,若有则数字重复(实质上也是暴力搜索)
/*  public:
    bool containsDuplicate(vector<int>& nums) {
        map<int, int> m1;
        for (auto i = nums.cbegin(); i != nums.cend(); i++) {
            auto pos = m1.find(*i);
            if(pos != m1.end()){
                return true;
            }else {
                m1[*i] = 1;
            }
        }
        return false;
    }*/
    //方法二(更优!):先对数组进行排序(nlogn),再遍历排序后数组,比较相邻的两个数是否相同
    public:
    bool containsDuplicate(vector<int>& nums) {
        if(nums.empty())
            return false;
        sort(nums.begin(), nums.end()
        );
        int temp = nums.front();
        int flag = 0;
        for(int n : nums) {
            if(n==temp&&flag==1)
                return true;
            else
                temp=n;
            flag=1;
        }
        return false;
    }
};

原文地址:https://www.cnblogs.com/esperanza/p/12160777.html

时间: 2024-10-31 09:57:22

LeetCode刷题记录(1)—— 217. 存在重复元素的相关文章

leetcode刷题记录(2)

301. Remove Invalid Parentheses Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()&q

Leetcode刷题记录[python]——344 Reverse String

一.前言 不是计算机专业出身,却有一颗程序猿的心. 昨日开始leetcode第一次刷题,选择了菜鸟方式,从AC率最高且难度为Easy的题开始,不管题是简单还是难,都想做个记录,既是方便以后回顾,又是以此作为一个激励,督促自己每天都能有所进步. 二.题344 Reverse String Write a function that takes a string as input and returns the string reversed. class Solution(object): def

Leetcode刷题记录[python]——349 Intersection of Two Arrays

一.前言 做了两题才慢慢摸清了leetcode的操作. 二.题349 Intersection of Two Arrays Given two arrays, write a function to compute their intersection. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]

Leetcode刷题记录[python]——283 Move Zeroes

一.前言 题是上周五做的,开始思路有点问题,考虑不全,导致submit了3次才AC. 二.题283 Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after call

leetcode刷题记录(JAVA&amp;Python)

---恢复内容开始--- --题目导航见页面左上角的悬浮框#目录导航#-- 相似题型: 1.1 twosum两数之和   2.2 3Sum三数之和 一.简单 1.1 twosum两数之和 原题: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 暴力解

leetcode刷题记录-Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

Leetcode刷题记录[python]——258 Add Digits

一.前言 做这题有个小收获,关于Digital root的解法,有个极方便的小公式: 二.题258 Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. class Solution(object): def addDigits(self, num): """ :type num: int :rtype: i

Leetcode 刷题记录 &amp; VLIS学长们的战绩

November 28, 2014 Leetcode全垒打! 以此纪念.找工作的路还是很漫长.继续奋斗.有学长学姐们的督促,必须更加努力. http://mcds.cs.cmu.edu/?q=node/26 2013 Chun Chen (Twitter, San Francisco, CA) Yiwen Chen (GOOGLE, Mt. View, CA) Mengwei Ding (GOOGLE, Mt. View, CA) Ge Gao (GOOGLE, Mt. View, CA) Yu

Leetcode刷题记录[python]——104 Maximum Depth of Binary Tree

一.前言 对于这类抽象问题还有self用法,还要多做多练习,此题是参考其他答案所做. 二.题104 Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. # Definitio