【LeetCode】27 - Remove Element
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.
Solution 1:
int removeElement(vector<int>& nums, int val)
{
/*set a new indexto to record new length, if it is val then jump to next element and do nothing,
else, keep it. But there are some redundant assignment*/
if(nums.empty())return ;
int length=;
for(int i=;i<nums.size();i++)
{
if(nums[i]!=val)
nums[length++]=val;
}
return length;
}
Solution 2:
int removeElement(vector<int>& nums, int val)
{
/*the most beautiful solution, when it meets val, assignment this position with the last element, it cost less than once tranversal
Tips: the element from the last may be val, so i-- is necessary*/
if(nums.empty())return ;
int newlength=nums.size();
for(int i=;i<newlength;i++)
{
if(nums[i]==val)
{
nums[i]=nums[newlength-];
i--;
newlength--;
}
}
return newlength;
}
【LeetCode】27 - Remove Element的更多相关文章
- 【LeetCode】27. Remove Element (2 solutions)
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 【LeetCode】027. Remove Element
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- 【easy】27. Remove Element
删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
随机推荐
- Android java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver
java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor ver 解决 ...
- OSI
1.物理(硬:HUB位) *****************信道接口型状.尺寸.引脚.排列电压.电流.阻抗.波形.速率及平衡单.半双.全双工RS232,RS422,RS423,RS485X.25.X. ...
- 《Linux内核设计与实现》读书笔记(十一)- 定时器和时间管理【转】
转自:http://www.cnblogs.com/wang_yb/archive/2013/05/10/3070373.html 系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务 ...
- Hibernate 异常 —— No CurrentSessionContext configured
在使用 SessionFactory 的 getCurrentSession 方法时遇到如下异常 “No CurrentSessionContext configured ” 原因是: 在hibern ...
- env1
给新手参考的Cadence Allegro快捷键 原文链接:http://www.eda365.com/thread-30935-1-1.html 发上我的快捷键给新手参考,我也是在LULU给我的EN ...
- java 反编译插件 JD-Eclipse 和 JD-IntelliJ
去官网 : http://jd.benow.ca/ 找到 JD-Eclipse 和 JD-IntelliJ,下载,前者安在eclipse上,后者安在as上.
- 加密解密(5)SSL形象比喻
转自: http://blog.csdn.net/cloverphp/article/details/11737433 前言: 关于公钥,私钥请看前几篇文章 SSL 协议既用到了公钥加密技术(握手 ...
- 不同浏览器JS获取浏览器高度和宽度
摘自:http://blog.csdn.net/lai_gb/archive/2009/07/04/4320956.aspx IE中: document.body.clientWidth ==> ...
- How to install Node.js on Linux
How to install Node.js on Linux Posted on November 13, 2015 by Dan Nanni Leave a comment Question: H ...
- Android View绘制流程
框架分析 在之前的下拉刷新中,小结过触屏消息先到WindowManagerService(Wms)然后顺次传递给ViewRoot(派生自Handler),经decor view到Activity再传递 ...