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 by modifying the input array in-place with O(1) extra memory.

  The order of elements can be changed. It doesn't matter what you leave beyond the new length.

  Example:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

分析:in-place就地remove元素,不允许分配另一个数组。可以这么想,允许用另一个数组的话,直接遍历这个数组,满足条件就添加到另一个数组中。但是不让用多一个数组的话,就用自身,因为满足条件的数量肯定是小于等于数组的长度,所以用一个index来计数即可,满足条件的放到index位置上,index++。
class Solution {
public int removeElement(int[] nums, int val) {
int index=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=val){
nums[index]=nums[i];
index++;
}
}
return index; } }

Leetcode 27——Remove Element的更多相关文章

  1. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  2. LeetCode 27. Remove Element (移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  3. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  4. LeetCode 27 Remove Element

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

  5. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...

  6. Java [leetcode 27]Remove Element

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

  7. (双指针) leetcode 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  8. Leetcode 27. Remove Element(too easy)

    Given an array and a value, remove all instances of that value in-place and return the new length. D ...

  9. [leetcode]27. Remove Element删除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

随机推荐

  1. freemarker之数组(十八)

    1.设计思路 (1)声明一个数组 (2)打印数组中的元素 2.设计源码 <#--freemarker数组--> <#assign nums=[12,34,56,78,90,54,23 ...

  2. 关于js中 toFixed()的一个小坑

    作为一名前端,大家都应该知道,toFixed()的作用,toFixed()经常用于前台与后台数据格式的转换,套用下w3c上面的定义: 定义和用法toFixed(n) 方法可把 Number 四舍五入为 ...

  3. pycharm short-cut

    Ctrl + the left mouse button Ctrl + Alt + Left/Right if invalid, system setting

  4. tablesorter 的使用

    <table id="myTable" class="tablesorter"> <thead> <tr> <th&g ...

  5. Python基础__函数

    本节将进入函数的介绍,函数是Python基础中最精彩的部分之一,接下来将对函数做详细介绍.函数 函数就是对代码进行一个封装.把实现某一功能的代码进行封装到一起.下次需要使用时不需要进行编写代码直接调用 ...

  6. Angular通过订阅观察者对象实现不同组件中数据的实时传递

    在angular官方定义中,组件直接的数据交换只要在父子直接传递,但是我们在项目中经常需要在各种层级之间传递数据,下面介绍关于订阅可观察对象实现的数据传递. 首先定义一个服务app.sevice.ts ...

  7. SEO优化策略

    原文:http://www.upwqy.com/details/186.html 1 首先了解seo是什么 SEO是英文Search Engine Optimization的缩写,中文译为" ...

  8. Sqlite 梳理

    前言 LZ最近失业,梳理下最近一个项目的 项目结构-供应链系统 接着上一篇博客 http://www.cnblogs.com/buruainiaaaa/p/6786527.html 上篇说到整套系统分 ...

  9. 怎么用secureCRT连接Linux

    首先要安装linux,参看:http://www.cnblogs.com/shenjieblog/p/5061282.html 然后要安装secureCRT,参看:http://www.cnblogs ...

  10. css 如何隐藏滚动条

    原理: 把滚动条设为完全透明: /* 设置滚动条的样式 */::-webkit-scrollbar { width: 12px;} /* 滚动槽 */::-webkit-scrollbar-track ...