博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Remove Element
阅读量:7111 次
发布时间:2019-06-28

本文共 738 字,大约阅读时间需要 2 分钟。

Remove Element

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.

思路

用双指针,一个代表当前的长度,另一个往后找和val不相同的

代码

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

转载地址:http://dvmhl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
快手通讯录
查看>>
我的友情链接
查看>>
LAMP搭建12:Apache配置防盗链
查看>>
java poi读取Excel数据 插入到SQL SERVER数据库中
查看>>
解决RecyclerView刷新时闪烁的问题,如何局部刷新列表项的某几个控件
查看>>
Failed to start component [StandardEngine[Catalin
查看>>
spring data mongodb 设置副本集读写分离模式
查看>>
Android Studio 一个简单的非传统Jni例子
查看>>
sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
查看>>
生产性能运维监控之TOP介绍
查看>>
如何利用边缘计算,实现低延时、高质量的互动课堂体验?
查看>>
第二次课程作业
查看>>
我的友情链接
查看>>
Linux 目录和文件管理
查看>>
svn安装与基本错误
查看>>
Mac Eclipse项目转换到AndroidStudio项目
查看>>
详解Node.js API系列 Crypto加密模块(1)
查看>>
可用于权限计算的帮助类
查看>>
如何在samba服务器上添加用户
查看>>