这道题跟26题很类似,并且有官方的答案。看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄。有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms。

iven 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 = [3,2,2,3],val = 3

你的函数应该返回length = 2,num的前两个元素为2。


我的实现算法:

 class Solution {
public int removeElement(int[] nums, int val) {
int index=1,count1=0,count2=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==val){
for(int j=i;j<nums.length;j++){
int temp=0;
if(nums[j]!=val){
temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
count2++;
break;
}
}
}
else
count1++;
}
return count1+count2;
}
}

官方的实现算法:

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

LeetCode记录之27——Remove Element的更多相关文章

  1. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  2. [LeetCode&Python] Problem 27. Remove Element

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

  3. LeetCode Array Easy 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❤python】27. Remove Element

    #-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        "& ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

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

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

  7. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  8. Leetcode 题目整理-7 Remove Element & Implement strStr()

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

  9. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

随机推荐

  1. 虚拟机安装CentOS以及SecureCRT设置【完美无错版】

    一.CentOS简介 CentOS是Linux的发行版之一,它安全.稳定.高效,是我最喜欢的Linux发行版之一.CentOS根据Red Hat Enterprise Linux开放源代码编译而成,与 ...

  2. Docker学习笔记_Dockerfile基本知识

    Dockerfile由一行行命令语句组成,并支持以#开头的注释行. 1.编写一个Dockerfile文件 创建一个空的Docker工作目录,进入该目录,使用sudo vim Dockerfile指令新 ...

  3. 7.ORDER BY 子句

    ORDER BY 语句 ORDER BY 语句用于根据指定的列对结果集进行排序. ORDER BY 语句默认按照升序对记录进行排序. 如果您希望按照降序对记录进行排序,可以使用 DESC 关键字. 1 ...

  4. Luogu 4254 [JSOI2008]Blue Mary开公司

    BZOJ 1568 学习了一波李超线段树. 大佬blog 这个东西专门用来维护插入一条线段和区间/单点的最大/最小值. 插入的时候讨论: 1.如果当前结点上没有线段,那么直接插入. 2.如果当前结点上 ...

  5. 小学四则运算生成器(Java) 刘少允,梁新男

    github传送门 项目相关要求 使用 -n 参数控制生成题目的个数.(实现) 使用 -r 参数控制题目中数值(自然数.真分数和真分数分母)的范围.(实现) 生成的题目中计算过程不能产生负数.(实现) ...

  6. springcloud集成swaggerui做动态接口文档

    1.配置文件pom,一定要使用2.4以上的,2.4默认请求方式是json,会导致getmapping设置参数类型对对象时,swaggerui界面不能指定为其他类型,反正就是各种坑,不建议用 <d ...

  7. WebService 天气预报webservice接口

    WebService  天气预报webservice接口 地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 常用接口: 1. ...

  8. 《C#多线程编程实战》2.4 SemaphoreSlim

    这个简单多了. 理解也是很好理解. 比上一个mutex好理解多了. 这个SemaphoreSlim是干什么呢? 就是限制线程的来访问. 好比说一次只有两个,一次只有三个  这样的线程来访问资源. 有点 ...

  9. 20165219 2017-2018-2 《Java程序设计》第5周学习总结

    20165219 2017-2018-2 <Java程序设计>第5周学习总结 课本知识总结 第7章 内部类与异常类 一 1 内部类:类的一种成员 2 外嵌类:包含内部类的类称为内部类的外嵌 ...

  10. Atcoder Grand Contest 032C(欧拉回路,DFS判环)

    #include<bits/stdc++.h>using namespace std;int vis[100007];vector<int>v[100007];vector&l ...