LeetCode记录之27——Remove Element
这道题跟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的更多相关文章
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- [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 ...
- 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 ...
- 【leetcode❤python】27. Remove Element
#-*- coding: UTF-8 -*- class Solution(object): def removeElement(self, nums, val): "& ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 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 ...
- 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 ...
随机推荐
- SpringAOP02 自定义注解
1 自定义注解 1.1 创建自定义注解 从java5开始就可以利用 @interface 来定义自定义注解 技巧01:注解不能直接干扰程序代码的运行(即:注解的增加和删除操作后,代码都可以正常运行) ...
- ubuntu 14 编译ARM g2o-20160424
1. 安装eigen sudo apt-get install libeigen3-dev sudo apt-get install libsuitesparse-dev sudo apt-get i ...
- ubuntu 设置虚拟机和主机在同一网段
一.在VMware中将网络连接方式设置为桥接 1.打开VM菜单栏->Settings 2.在弹出的Virtual Machine Setting对话框中点击Network Adapter,在右边 ...
- Monkey稳定性测试环境搭建说明
一.安装Java环境 安装Java环境-JDK:下载地址:http://pan.baidu.com/s/1pJ6Yqs7,jdk安装解压即可. 二.设置环境变量 双击下载的JDK ,设置安装路径.这里 ...
- MongoDB整理笔记の增加节点
MongoDB Replica Sets 不仅提供高可用性的解决方案,它也同时提供负载均衡的解决方案,增减Replica Sets 节点在实际应用中非常普遍,例如当应用的读压力暴增时,3 台节点的环境 ...
- 数据库去重与join连表
join连表删除的效率与检测存在之后删除的效率比,后者的效率低了很多
- Android Service基本知识总结(一)
一.简介 Service是Android系统的后台服务组件,适用于开发无界面.长时间运行的应用功能Service特点如下: 没有用户界面 不会轻易被Android系统终止 在系统资源恢复后Servic ...
- 使用Sencha Cmd创建脚本框架
从Ext JS 4.1.1a 开始,为了配合 Sencha Touch开发 而设计了 Sencha Cmd这个跨平台的命令行工具. 要使用Sencha Cmd,必须先安装好 Java Run-tim ...
- 国内物联网平台(8):中移物联网开放平台OneNet
国内物联网平台(8)——中移物联网开放平台OneNet 马智 平台定位 OneNET是中移物联网有限公司搭建的开放.共赢设备云平台,为各种跨平台物联网应用.行业解决方案,提供简便的云端接入.存储.计算 ...
- Linux中的SELinux与chcon以及Samba实现【转】
一.SELinux SElinux的前身是NSA(美国国家安全局)发起的一个项目.它的目的是将系统加固到可以达到军方级别. 为什么NSA选择Linux呢? 在目前市面上大多数操作系统都是商用闭源的,只 ...