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 ...
随机推荐
- 2014蓝桥杯B组初赛试题《六角填数》
题目描述: 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交 ...
- C++获取时间并命名为文件名
#include <time.h> char pStrPath1[20];time_t currTime;struct tm *mt;Mat saveImg1; currTime = ti ...
- Mac下MongoDB enterprise版的安装
1. 访问mongodb下载中心,https://www.mongodb.com/download-center#enterprise,选择OS X x64系统,点击下载,可能会出一个页面让你填写联系 ...
- SQLAlchemy(ORM框架)
SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python mysql+mysqldb://<user>:&l ...
- Part5核心初始化_lesson4---关闭mmu
1.ARM存储体系 2.cache 3.虚拟地址 那么谁来完成把虚拟地址转换成物理地址呢? 4.这个工作就由MMU来转换!! 5.关闭MMU和cache 他们都是通过cp15协处理器来控制的!应该在A ...
- wireshark抓取qq数据包
抓包接口设置成本地连接 点击start,登录qq,输入oicq进行过滤qq包 找到第一个OICQ,点击后,点击oicq-IM software 可以看到自己登录的QQ号码为765343409 本机IP ...
- [GO]数组指针做函数参数
package main import "fmt" //p指向实现数组a,它是指向数组,它是数组指针//*p指向指针指向的内存,就是实参a func modify1(p *[]in ...
- win7,64bit下的OpenGL着色语言(glsl)开发环境配置(原)
一.环境准备: 系统环境win7,64位,双显卡:集成显卡+gt540m,gt540建议下载最新的驱动,可以支持到opengl4.3标准,一般双显的笔记本,程序默认启用的是集显,我机器的集显驱动有点老 ...
- NSString 对象保存在哪? @“xxx”和 stringWithFormat:@"xxx" 区别?
NSString *str1=@"string";//这种是保存在常量池 NSString *str2=@"string"; NSLog(@"str1 ...
- oracle 中 创建序列sequence
drop sequence SEQ_YCXWP_CGD; create sequence SEQ_YCXWP_CGD increment start nomaxvalue;