zoukankan      html  css  js  c++  java
  • 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 /*************************************************************************
     2     > File Name: LeetCode217.c
     3     > Author: Juntaran
     4     > Mail: Jacinthmail@gmail.com
     5     > Created Time: Tue 10 May 2016 03:15:05 PM CST
     6  ************************************************************************/
     7  
     8 /*************************************************************************
     9     
    10     Contains Duplicate
    11     
    12     Given an array of integers, find if the array contains any duplicates. 
    13     Your function should return true if any value appears at least twice in the array, 
    14     and it should return false if every element is distinct.
    15     
    16  ************************************************************************/
    17 
    18 #include <stdio.h>
    19 
    20 void quick_sort( int* nums, int left, int right )
    21 {    
    22     if( left < right )
    23     {
    24         int i = left;
    25         int j = right;
    26         int flag = nums[left];
    27         
    28         while( i < j )
    29         {
    30             while( i<j && nums[j]>=flag )    // 从右向左找第一个小于x的数  
    31             {
    32                 j--;
    33             }
    34             if( i < j )
    35             {
    36                 nums[i++] = nums[j];
    37             }
    38         
    39             while( i<j && nums[i]<flag )    // 从左向右找第一个大于等于x的数
    40             {
    41                 i++;
    42             }
    43             if( i < j )
    44             {
    45                 nums[j--] = nums[i];
    46             }
    47         }  
    48         nums[i] = flag;
    49         quick_sort( nums, left, i-1 );
    50         quick_sort( nums, i+1, right);
    51     }
    52 }
    53 
    54 int containsDuplicate( int* nums, int numsSize )
    55 {
    56     int ret = 0;
    57     if( numsSize < 2 )
    58     {
    59         ret = 0; 
    60     }
    61     quick_sort( nums, 0, numsSize-1 );
    62     printfNums( nums, numsSize );
    63     
    64     int i;
    65     for( i=0; i<numsSize; i++ )
    66     {
    67         if( nums[i] == nums[i+1] )
    68         {
    69             ret = 1;
    70         }
    71     }
    72     printf("
    %d
    ", ret);
    73     return ret;
    74 }
    75 
    76 void printfNums( int* nums, int numsSize )
    77 {
    78     int i;
    79 
    80     for( i=0; i<numsSize; i++ )
    81     {
    82         printf("%d ", nums[i]);
    83     }
    84 }
    85 
    86 int main()
    87 {
    88     int nums[] = { 9,8,7,6,5,4,3,2,1,0 };
    89     int numsSize = 10;
    90     containsDuplicate( nums, numsSize );    
    91     
    92     return 0;
    93 }
  • 相关阅读:
    常见图片格式PNG,JPEG,BMP,GIF区别总结
    sql在所有存储过程中查询包含某字符串的执行语句
    数字取整或保留小数四舍五入的正确写法
    SVG路径path的贝塞尔曲线指令
    查询总耗CPU最多与平均耗CPU最多的SQL语句
    MIME 参考手册
    SQL语句复制父子级表数据
    去掉数字格式结尾多余的零,补充数字格式结尾需要的零
    设置微信分享的标题 缩略图 连接 描述
    linux环境下php开启redis扩展(centos6.8)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5479095.html
Copyright © 2011-2022 走看看