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

    简直就是神来之笔~~~



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

  • 相关阅读:
    javascript调用applet
    mysql“Access denied for user 'root'@'localhost'”问题的解决
    VS2010 加载Dll文件
    预处理符号
    什么是lib文件,lib和dll的关系如何[转]
    git常用命令
    VC项目配置基础[转]
    [转]Linux ftp命令的使用方法
    [转]JavaScript创建Applet 标签的属性介绍 以及 Applet调用JavaScript
    When you publish a workflow in Microsoft Dynamics CRM 4.0 after you install Update Rollup 2, you receive Error message
  • 原文地址:https://www.cnblogs.com/learnordie/p/4656962.html
Copyright © 2011-2022 走看看