zoukankan      html  css  js  c++  java
  • Remove Element

    package cn.edu.xidian.sselab.array;

    /**
     *
     * @author zhiyong wang
     * title:Remove Element
     * content:
     * 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.
     *
     */
    public class RemoveElement {

        //这个题与MoveZeroes是相同的思路,MoveZeroes是将所有的0移动到数组的最后面,
        //而本题目是将val值全被替换掉,此时的val换成0,就跟MoveZeroes是完全一样的题目了
        public int removeElement(int[] nums, int val){
            int length = nums.length;
            int count = 0;
            for(int i=0;i<length;i++){
                if(nums[i] == val){
                    count++;
                }else{
                    nums[i-count] = nums[i];
                }
            }
            return length - count;
        }
    }

  • 相关阅读:
    高精度乘除运算优化
    高精度除法
    高精度乘法
    期末考试
    P2341 [HAOI2006]受欢迎的牛[SCC缩点]
    P2002 消息扩散[SCC缩点]
    神奇搜索算法A*
    P3205 [HNOI2010]合唱队[区间dp]
    P4170 [CQOI2007]涂色
    P1220 关路灯[区间dp]
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5061983.html
Copyright © 2011-2022 走看看