[US Giants] 二. Integer-Array

来源:互联网 发布:淘宝卖家快递推荐 编辑:程序博客网 时间:2024/06/03 21:47

Remove Element

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

The order of elements can be changed, and the elements after the new length don't matter.

Example

Given an array [0,4,4,0,0,2,4,4]value=4

return 4 and front four elements of the array is [0,0,0,2]

方法一

思路:i从开始遍历,只要是不等于elem的元素值,就挨着从index=0开始从前往后放,后面的就是都等于elem的
public class Solution {    public int removeElement(int[] A, int elem) {        int index=0;        for(int i=0;i<A.length;i++){            if(A[i]!=elem){               A[index++]=A[i];            }        }        return index;    }}

方法二

思路:i从前往后遍历,如果A[i]=elem,就和最后一个交换(即使最后一个也等于i也没关系,因为和下一个判断的时候还是要从i开始)

         如果不等于,就继续判断下一个

public class Solution {    /**      *@param A: A list of integers     *@param elem: An integer     *@return: The new length after remove     */    public int removeElement(int[] A, int elem) {        if(A==null || A.length==0){            return 0;        }                int i=0;        int j=A.length-1;        while(i<=j){            if(A[i]==elem){                A[i]=A[j];                j--;            }else{                i++;            }                    }        return i;    }}

50. Product of Array Exclude Itself

点击打开链接

思路:两趟循环,对于每一个output[i]={i前面的数的乘积}*{i后面的数的乘积}

         第一趟正向遍历数组,对于每个Xi计算Xo~Xi-1的连续乘积

         第二趟反向遍历数组,对于每个Xi计算Xn-1~Xi+1的连续乘积

例如:A={2,3,4};

         left={1,2,6}; left[1]=left[0]*2=2;  left[2]=left[1]*3=6

         right={12,4,1}; right[2]=1; right[1]=right[2]*4=4;  right[0]=right[1]*3=12

         output={12,8,6};

public class Solution {    /**     * @param A: Given an integers array A     * @return: A Long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]     */    public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) {        ArrayList<Long> result = new ArrayList<Long>();        if (A == null || A.size() == 0) {            return result;        }                Long[] left = new Long[A.size()];        left[0] = 1L;        for (int i = 1; i < A.size(); i++) {            left[i] = left[i - 1] * A.get(i - 1);        }                Long[] right = new Long[A.size()];        right[A.size() - 1] = 1L;        for (int i = A.size() - 2; i >= 0; i--) {            right[i] = right[i + 1] * A.get(i + 1);        }                for (int i = 0; i < A.size(); i++) {            result.add(left[i] * right[i]);        }        return result;    }}

189. First Missing Position

点击打开链接
思路:交换数组元素,使得数组第位存放元素i+1,也就是使得A[i]=i+1
例如:[1,2,0],由于i=2的位置是0,不是3,break,还是[1,2,0],下面for循环返回i+1是3
         [3,4,-1,1],交换数组位置后[1,-1,3,4],下面for循环到i=1,A[i]!=2,返回2
public class Solution {    /**         * @param A: an array of integers     * @return: an integer     */    public int firstMissingPositive(int[] A) {        if(A==null || A.length==0){            return 1;        }                  for(int i=0;i<A.length;i++){            while(A[i]>0 && A[i]<=A.length && A[i]!=i+1){                int temp=A[A[i]-1];                       //temp表示当前i应该在的位置的值,因为当前i最后会被交换到应该在的位置                if(temp==A[i]){                           //例如[-1,-1,1],遍历到i=2的时候,A[i]=1,而temp=A[0]=-1                    break;                }                A[A[i]-1]=A[i];                A[i]=temp;            }        }        for(int i=0;i<A.length;i++){            if(A[i]!=i+1){                return i+1;            }        }        return A.length+1;    }}



原创粉丝点击