• 技术文章 >Java >java教程

    LeetCode & Q27-Remove Element-Easy

    PHP中文网PHP中文网2017-07-10 18:13:15原创867
    Array Two Pointers

    Description:

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

    Do not allocate extra space for another array, you must do this in place with constant memory.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    Example:

    Given input array nums = [3,2,2,3], val = 3

    Your function should return length = 2, with the first two elements of nums being 2.

    my Solution:

    public class Solution {
        public int removeElement(int[] nums, int val) {
            int j = 0;
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] != val) {
                    nums[j++] = nums[i];
                }
            }
            return j++;
        }
    }

    以上就是LeetCode &amp; Q27-Remove Element-Easy的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:Java学习的路线规划 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • Java数据结构之单链表与OJ题• 详细介绍Java正则表达式之单字符匹配和预定义字符• Java总结分享之反射、枚举、Lambda表达式• 一起来分析java设计模式之单例• 一文搞懂Java线程池实现原理
    1/1

    PHP中文网