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 }
  • 相关阅读:
    路由器桥接是个什么玩法
    MAC使用小技巧之------用好mac电脑的10个必知的小技巧!
    学习笔记1--响应式网页+Bootstrap起步+全局CSS样式
    mysql运维必会的一些知识点整理
    面试小结1--填空题
    CSS技术实例1-使用CSS计数器实现数值计算小游戏实例页面
    编译8.0
    解决Windows 10 1809 使用管理员权限运行的程序无法浏览网络驱动器的问题
    android sdk
    酷卓教程 明明已经已经有了面具Magisk 确无法正常使用root权限
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511675.html
Copyright © 2011-2022 走看看