zoukankan      html  css  js  c++  java
  • Remove Element 分类: Leetcode(线性表) 2015-01-29 10:47 55人阅读 评论(0) 收藏

    Given an array and a value, remove all instances of that value in place and return the new length.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    这道题看起来很简单,最开始时想,把不要的数字全部换到数组末尾同时减少数组长度就好了

    class Solution {
    public:
      
        int removeElement(int A[], int n, int elem) {
            int i,j,p1,p2,tem;
            int len=n;
            if(n==0) return 0;
            if(n<=1) 
            {
                if(A[0]==elem) return n-1;
                else return n;
            }
           
       
            j=n-1;
            for(i=0;i<n;i++)
            {
                if(A[i]==elem)
                {
                    A[i]=A[j--];
                }
            }
            return j+1;
        }
    };

    结果证明Naive了,
    Input: [4,2,0,2,2,1,4,4,1,4,3,2], 4
    Output: [2,2,0,2,2,1,3,4]
    Expected: [2,2,0,2,2,1,3,1]

    可以看到这样处理的问题是,A[j]=elem的时候,换了跟没换是一样的,所以想增加A[j]!=elem

    class Solution {
    public:
      
        int removeElement(int A[], int n, int elem) {
            int i,j,p1,p2,tem;
            int len=n;
            if(n==0) return 0;
            if(n<=1) 
            {
                if(A[0]==elem) return n-1;
                else return n;
            }
           
       
            j=n-1;
            for(i=0;i<n;i++)
            {
                while(i<j && A[j--]==elem);
                if(A[i]==elem)
                {
                    A[i]=A[j];
                }
            }
            return j+1;
        }
    };
    结果出错了,{3 3 } 3这种情况 还是Naive,换个思路,先把数组排好序,Done

    class Solution {
    public:
      void qsort(int *A,int l,int r)
        {
            if(l<r){
            int i=l,j=r;
            int x=A[l];
            while(i<j)
            {
                while(i<j && A[j] >=x)
                {
                    j--;
                }
                if(i<j)
                {
                    A[i] = A[j];
                    i++;
                }
                while(i<j && A[i] <x)
                {
                    i++;
                }
                if(i<j)
                {
                    A[j] = A[i];
                    j--;
                }
            }
            A[i] = x;
            qsort(A,l,j-1);
            qsort(A,i+1,r);
            }
        }
      
        int removeElement(int A[], int n, int elem) {
            int i,j,p1,p2,tem;
            int len=n;
            if(n==0) return 0;
            if(n<=1) 
            {
                if(A[0]==elem) return n-1;
                else return n;
            }
            
            qsort(A,0,n-1);
       
            j=n-1;
            for(i=0;i<n;i++)
            {
                if(A[i]==elem)
                {
                    A[i]=A[j--];
                }
            }
            return j+1;
        }
    };

    其实用快速排序跑的时间比交换排序还要长,可能测试数据大多还是有序的。不过再想想能不能不排序,之前我们的思路一直是想对A[i]==elem的A[i]处理,现在换一种思路,对A[i]!=elem的处理,这样一样就出现了掉渣天的代码:

    class Solution {
    public:
      
        int removeElement(int A[], int n, int elem) {
            int i,j;
            int index=0;
            for(i=0;i<n;i++)
            {
                if(A[i]!=elem)
                {
                    A[index++]=A[i];
                }
            }
            return index;
        }
    };

    简直就是神来之笔~~~



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【学习笔记】斯特林数(未完成)
    【题解/学习笔记】点分树
    【题解】[国家集训队] Crash 的文明世界
    【题解】SP34096 DIVCNTK
    【题解】Loj6053 简单的函数
    【题解】[ZJOI2012]网络
    【题解】bzoj3252 攻略
    【题解】[POI2014]HOT-Hotels 加强版
    IDEA文件夹变红,轻松删除SVN版本控制关联
    配置dataguard broker并主从切换
  • 原文地址:https://www.cnblogs.com/learnordie/p/4656962.html
Copyright © 2011-2022 走看看