博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
27. Remove Element
阅读量:4710 次
发布时间:2019-06-10

本文共 808 字,大约阅读时间需要 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.

好水的题目。

class Solution {public:    int removeElement(vector
& nums, int val) { int len = nums.size(); if (len == 0) return 0; int cnt = 0; for (int i = 0; i < len; ++i) { if (nums[i] == val) { } else nums[cnt ++] = nums[i]; } return cnt; }};

 

转载于:https://www.cnblogs.com/pk28/p/7201030.html

你可能感兴趣的文章
关于动态生成data组件
查看>>
Docker镜像优化
查看>>
win7下mysql8.0.12解压缩版安装
查看>>
Linux Tomcat
查看>>
刷题的第一天。【并不是
查看>>
java类型生命周期
查看>>
asp.net Mvc Npoi 导出导入 excel
查看>>
vue-cli脚手架搭建项目整理笔记
查看>>
详细设计3天学习笔记
查看>>
在centos6.5 上安装最新版mysql
查看>>
使用代理模式来给交易模块添加缓存功能
查看>>
HTML5 and CSS3 开发
查看>>
金字塔 (动态规划)
查看>>
HDU 3518
查看>>
201671010110 2016 2017 2《java程序设计》
查看>>
Leetcode892.Surface Area of 3D Shapes三维形体的表面积
查看>>
剑指offer——二叉树的下一个节点
查看>>
MyEssay 之 Python正则表达式 —— 四种断言扩展的理解
查看>>
代码管理工具Git的安装及使用
查看>>
JAVA socket
查看>>