zoukankan      html  css  js  c++  java
  • LeetCode 80

    Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":
    What if duplicates are allowed at most twice?

    For example,
    Given sorted array nums = [1,1,1,2,2,3],

    Your function should return length = 5,
    with the first five elements of nums being 1, 1, 2, 2 and 3.
    It doesn't matter what you leave beyond the new length.

     1 /*************************************************************************
     2     > File Name: LeetCode080.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 17 May 2016 20:54:02 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Remove Duplicates from Sorted Array II
    11     
    12     Follow up for "Remove Duplicates":
    13     What if duplicates are allowed at most twice?
    14 
    15     For example,
    16     Given sorted array nums = [1,1,1,2,2,3],
    17 
    18     Your function should return length = 5, 
    19     with the first five elements of nums being 1, 1, 2, 2 and 3. 
    20     It doesn't matter what you leave beyond the new length.
    21 
    22  ************************************************************************/
    23 
    24 #include <stdio.h>
    25 
    26 int removeDuplicates(int* nums, int numsSize)
    27 {
    28     if(numsSize < 2)
    29     {
    30         return numsSize;
    31     }
    32 
    33     int count = 1;
    34     int j = 1;
    35     int i;
    36 
    37     for( i=1; i<numsSize; i++ )
    38     {
    39         if( nums[i] == nums[i-1] )
    40         {
    41             count ++;
    42             if( count < 3 )
    43             {
    44                 nums[j++] = nums[i];
    45             }
    46             else
    47             {
    48                 printf("***
    ");
    49             }
    50             printf("i=%d j=%d nums[i]=%d nums[j]=%d
    ", i,j,nums[i],nums[j]);
    51             printfNums(nums,numsSize);
    52         }
    53         else
    54         {
    55             nums[j++] = nums[i];
    56             count = 1;
    57             printf("i=%d j=%d nums[i]=%d nums[j]=%d
    ", i,j,nums[i],nums[j]);
    58             printfNums(nums,numsSize);
    59         }
    60     }
    61     return j;
    62 
    63 }
    64 
    65 void printfNums( int* nums, int numsSize )
    66 {
    67     int i;
    68     for( i=0; i<numsSize; i++ )
    69     {
    70         printf("%d ", nums[i]);
    71     }
    72     printf("
    ");
    73 }
    74 
    75 int main()
    76 {
    77     int nums[] = { 1, 2, 3, 3, 3, 5, 5, 5 };
    78     int numsSize = 8;
    79 
    80     int ret = removeDuplicates( nums, numsSize );
    81     printf("%d
    ", ret);
    82 }
  • 相关阅读:
    js数组
    关于编程,程序员的一些语录
    css心得
    js函数
    一些电脑基础知识
    gnome3安装
    C学习小记
    ubuntu重装系统后
    elinks文字浏览器
    快捷方式
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511675.html
Copyright © 2011-2022 走看看