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

  • 相关阅读:
    链表数据-PHP的实现
    关于go的init函数
    socket小计
    很随笔
    go获取当前项目下所有依赖包
    关于synergy的问题
    二叉树的最大路径和
    大数求和
    重载<<运算符第二个参数必须加上const
    表达式求值
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5061983.html
Copyright © 2011-2022 走看看