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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  3. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  4. 【LeetCode】027. Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  5. 【easy】27. Remove Element

    删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the ...

  6. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  7. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  8. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  9. 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 ...

随机推荐

  1. C# 获取打印机状态

    public static string GetPrinterStatus(string PrinterName)    {    int intValue = GetPrinterStatusInt ...

  2. MTK Android 编译命令

    一.Target 编译命令 usage: (makeMtk|mk) [options] project actions [moudles] options:       -t,-tcc         ...

  3. hibernate学习笔记6--Criteria查询方式、完整小练习(开发步骤)

    一.Criteria查询方式没有sql语了,因此更加面向对象一些.Criteria是一种比HQL更面向对象的查询方式:Criteria的创建方式: Criteria c = s.createCrite ...

  4. Qt之QHeaderView自定义排序(终极版)

    简述 本节主要解决自定义排序衍生的第二个问题-将整形显示为字符串,而排序依然正常. 下面我们介绍三种方案: 委托绘制 用户数据 辅助列 很多人也许会有疑虑,平时都用delegate来绘制各种按钮.图标 ...

  5. HDU (线段树 单点更新) 敌兵布阵

    哎,又切了一天的水题. 线段树果然必须自己写出来才能叫真正的会了,之前一直在套模板确实不好. 这个题目是单点更新 之 单点增减,= ̄ω ̄= #include <cstdio> <&l ...

  6. POJ2886 Who Gets the Most Candies? 线段树 反素数

    题意:有一群小朋友围成一个环,编号1,2,3…N.每个人手上握着一个非0的数字,首先第K个人出列,然后看他手上的数字,假设为m,则从下一个开始第m个人出列,一直如此.并设i为小于等于N的最大反素数,问 ...

  7. Zookeeper工作原理

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  8. 【转】Qt多线程操作界面---在QThread更新QProgressBar

    #include <QApplication> #include <QThread> #include <QMainWindow> #include <QPr ...

  9. python练习程序(c100经典例3)

    题目: 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? for i in range(1,100000): a=i+100; b=a+168; sa=int ...

  10. 《Unix网络编程》卷2 读书笔记 第2章- Posix IPC

    1. 概述 Posix IPC 包括:Posix消息队列.Posix信号量.Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点. 本章讲述所有这些共同属性:用于标识 ...