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;
        }
    };

    简直就是神来之笔~~~



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

  • 相关阅读:
    创业者要有杀手气质和传教士能力
    一次只专心地做一件事,全身心地投入并积极地希望它成功
    以变应变,才有出路
    得到的并不一定就值得庆幸,失去的也并不完全是坏事情
    独处可以激发思考的力量
    把情感装入理性之盒
    随着现实的变化,我们必须随之调整自己的观念、思想、行动及目标
    岁月在变迁,彼此在成长。而我在流浪
    [TJOI 2016&HEOI 2016]排序
    [HAOI 2008]糖果传递
  • 原文地址:https://www.cnblogs.com/learnordie/p/4656962.html
Copyright © 2011-2022 走看看