博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
27. Given an array and a value
阅读量:3574 次
发布时间:2019-05-20

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

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.

不得不吐槽这原题题意混乱。。。

遍历 nums,遇到与 val 不同的值,把 i 位置的值提到 length 位置去,长度加一。代码如下:

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

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

你可能感兴趣的文章
linux VM虚拟机可以ping通主机,但主机无法ping通虚拟机
查看>>
C++ 中Struct与typedef struct总结
查看>>
WNetAddConnection2调用失败,错误码1200/1312
查看>>
POI读写Excel的基本使用
查看>>
淘宝网站的架构演进
查看>>
设置zookeeper开机自启动流程
查看>>
CentOS安装mysql5.7的教详细流程
查看>>
项目整合微信扫码登录功能
查看>>
分布式文件系统FastDfs的搭建
查看>>
Springboot项目利用Java客户端调用FastDFS
查看>>
全文检索工具elasticsearch的安装和简单介绍
查看>>
利用Kibana学习全文检索工具elasticsearch
查看>>
SpringBoot在Test测试类或自定义类中通过@Autowired注入为null
查看>>
使用docker搭建YAPI服务
查看>>
西南科技大学OJ题 邻接表到邻接矩阵1056
查看>>
西南科技大学OJ题 有向图的出度计算1057
查看>>
西南科技大学OJ题 有向图的最大出度计算1059
查看>>
西南科技大学OJ题 带权有向图计算1063
查看>>
oracle主键自增触发器编写
查看>>
String与StringBuilder与StringBuffer三者的差别
查看>>