leetcode 217. Contains Duplicate

来源:互联网 发布:足球指数分析软件 编辑:程序博客网 时间:2024/05/23 11:56

leetcode 217. Contains Duplicate

题目

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

解1:两次循环,超时

public class Solution {    public boolean containsDuplicate(int[] nums)    {        两次循环         for(int i=0;i<nums.length;i++){             for(int j=0;j<i;j++){                 if(nums[i]==nums[j]){                     return true;                 }             }          }          return false;   }}       

解2:利用HashSet,因为其数组元素是不重复的,超时

public class Solution {    public boolean containsDuplicate(int[] nums)    {        //利用HashSet,数组元素是不重复的        Set<Integer> integers=new HashSet<Integer>();         for(int i:nums){             if(integers.contains(i))return true;             else integers.add(i);         }         return false;    }}

解3:利用自己写的冒泡排序函数,超时

其实这个时候,先排序,再让临近元素相比较的想法已经很对了,但是,要考虑排序算法的时间复杂度问题。

public class Solution {    public boolean containsDuplicate(int[] nums)    {         //调用自己写的冒泡排序        int n = nums.length;        sort1(nums);              int i = 1;        while(i < n){            if(nums[i] == nums[i-1])                return true;            i++;         }        return false;    }}  //冒泡排序    public void sort1(int[] a)    {        int temp = 0;        for (int i = a.length - 1; i > 0; --i)        {            for (int j = 0; j < i; ++j)            {                if (a[j + 1] < a[j])                {                    temp = a[j];                    a[j] = a[j + 1];                    a[j + 1] = temp;                }            }        }    }

解4:调用自己写的归并排序函数,通过

目前最稳定的排序算法,最差时间复杂度,最好时间复杂度,平均时间复杂度都是O(nlogn),并且是稳定的。

public class Solution {    public boolean containsDuplicate(int[] nums)    {        //调用自己写的归并排序函数        int n = nums.length;                    sort(nums, 0, n-1);        int i = 1;        while(i < n){            if(nums[i] == nums[i-1])                return true;            i++;        }        return false;     }    /**归并排序函数     *      * @param a     * @param s     * @param len     * 每次归并的有序集合的长度     */   public static int[] sort(int[] nums, int low, int high) {          int mid = (low + high) / 2;          if (low < high) {              // 左边              sort(nums, low, mid);              // 右边              sort(nums, mid + 1, high);              // 左右归并              merge(nums, low, mid, high);          }          return nums;      }    public static void merge(int[] nums, int low, int mid, int high) {          int[] temp = new int[high - low + 1];          int i = low;// 左指针          int j = mid + 1;// 右指针          int k = 0;          // 把较小的数先移到新数组中          while (i <= mid && j <= high) {              if (nums[i] < nums[j]) {                  temp[k++] = nums[i++];              } else {                  temp[k++] = nums[j++];              }          }          // 把左边剩余的数移入数组          while (i <= mid) {              temp[k++] = nums[i++];          }          // 把右边边剩余的数移入数组          while (j <= high) {              temp[k++] = nums[j++];          }          // 把新数组中的数覆盖nums数组          for (int k2 = 0; k2 < temp.length; k2++) {              nums[k2 + low] = temp[k2];          }      }    }

解5:调用java自带的排序函数,通过

1,Arrays.sort(int[] arr,int start ,int end); 其中start是开始下标,end是结束下标加1(可以写成数据长度,arr.length);
2,网上搜索得知:JDK1.6中Array.sort()采用的是归并排序,而JDK1.7中采用的是TimSort排序(TimSort是归并排序的优化版本)。

public class Solution {    public boolean containsDuplicate(int[] nums)    {        //调用java自带sort的函数        int n = nums.length;                    Arrays.sort(nums, 0, n);        int i = 1;        while(i < n){            if(nums[i] == nums[i-1])                return true;            i++;        }        return false;     }}

小结

1,第一次对排序算法这么感兴趣,好感人;
2,多了解语言自带的一些函数,会让写代码变得简单很多,比如现在我就知道,原来java是自己有对于int型数据的排序函数Arrays.sort(int[] arr,int start ,int end)的;
3,C++的对应排序函数是std::sort(nums.begin(), nums.end())。

0 0
原创粉丝点击